简体   繁体   English

启用 hover 以在移动设备上进行触摸/点击

[英]Enable hover for touch/click on mobile

Everything works the way it should on desktop view but the DIV which shows on hover in desktop doesn't show in mobile view.一切都在桌面视图上按其应有的方式工作,但在桌面上的 hover 上显示的 DIV 在移动视图中不显示。 I want to show that DIV on touch/click in mobile/tablet.我想在移动/平板电脑中通过触摸/点击显示该 DIV。

As per some suggestions, I tried with :active after :hover too but that doesn't work.根据一些建议,我也在:hover之后尝试了:active ,但这不起作用。

I don't mind JS or jQuery answers too.我也不介意 JS 或 jQuery 的答案。

DEMO: https://jsfiddle.net/ovg6xzhu/演示: https://jsfiddle.net/ovg6xzhu/

HTML: HTML:

<div class="hoverEffect hoverEffect-first">
  <img src="https://via.placeholder.com/150" />
  <div class="mask">
    <h2>Web Services!</h2>
    <p>We are going to build another sets of css hover image effects with CSS3 animations.<br /><a href="#">View All</a></p>
  </div>
</div>

CSS: CSS:

.hoverEffect {
  width: 100%;
  float: left;
  overflow: hidden;
  position: relative;
  text-align: center;
  cursor: default;
  border-radius: 2px;
  margin-bottom: 30px;
}
.hoverEffect .mask {
  width: 100%;
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0
}
.hoverEffect img {
  display: block;
  position: relative
}
.hoverEffect h2 {
  text-transform: uppercase;
  color: #fff;
  text-align: center;
  position: relative;
  padding: 10px;
  margin: 20px 0 0 0
}
.hoverEffect p {
  position: relative;
  color: #fff;
  padding: 0px 20px 20px;
  text-align: center
}
.hoverEffect-first img {
  transition: all 0.2s linear;
  width:100%;
  height:auto;
}
.hoverEffect-first .mask {
  opacity: 0;
  background-color: rgba(61, 90, 128, 0.95);
  transition: all 0.4s ease-in-out;
  width: 100%;
  height: 100%;
}
.hoverEffect-first h2 {
  margin: 10px 40px;
  transform: translateY(-100px);
  opacity: 0;
  transition: all 0.2s ease-in-out;
}
.hoverEffect-first p {
  transform: translateY(100px);
  opacity: 0;
  transition: all 0.2s linear;
}
.hoverEffect-first:hover img, .hoverEffect-first:active img {
  transform: scale(1.1);
}
.hoverEffect-first:hover .mask, .hoverEffect-first:active .mask {
  opacity: 1;
}
.hoverEffect-first:hover h2,
.hoverEffect-first:hover p,
.hoverEffect-first:active h2,
.hoverEffect-first:active p {
  opacity: 1;
  transform: translateY(0px);
}
.hoverEffect-first:hover p, .hoverEffect-first:active p {
  transition-delay: 0.1s;
}
.hoverEffect-first:hover a.info, .hoverEffect-first:active a.info {
  transition-delay: 0.2s;
}

Your code is working.您的代码正在运行。 Always make sure to clear your browser cache.始终确保清除浏览器缓存。 A quick trick to do this on mobile would be to add " /?random " at the end of your url to get a fresh load.在移动设备上执行此操作的一个快速技巧是在 url 的末尾添加“/?random”以获得新负载。 Make sure to change " random " to anything you wish per refresh so it keeps sending you a fresh load.确保每次刷新都将“随机”更改为您希望的任何内容,以便它不断向您发送新的负载。 That said I would also have to agree with ehab's answer, it's not very intuitive to show/hide your content upon hovering on mobile.也就是说,我也必须同意 ehab 的回答,在移动设备上悬停时显示/隐藏您的内容并不是很直观。 Add visual indicators like a button or let the animation take place naturally when it enters the viewport.添加按钮等视觉指示器或让 animation 在进入视口时自然发生。

You dont need js for this purpose: css should be enough, and active should do the job, The code you posted is working fine - maybe u had a cache problem.为此,您不需要 js: css 应该足够了,并且活动应该可以完成这项工作,您发布的代码工作正常 - 也许您遇到了缓存问题。 That being said its not the best experience for mobile users, you should not emulate hover effects on mobile, users wont expect they need to click ( and then they are expected to click on another element to make the effect go away).话虽如此,它对移动用户来说并不是最好的体验,你不应该在移动设备上模拟 hover 效果,用户不会期望他们需要点击(然后他们应该点击另一个元素以使效果 go 消失)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM