How to scroll down when hover an image?

荒凉一梦 提交于 2021-02-05 11:14:49

问题


I want to when hovering an image, it will scroll down to the end of the image.

I have two question:

  1. How to scroll to end of the image when user hover on it? Currently, I start hover on the image, it wasn't scrolled to end of the image.

  2. How to control the speed of scroll when hovering on an image?

My code:

body {
  margin: 2px auto;
  width: 500px;
}

.pic {
  width: 48%;
  height: 200px;
  overflow: hidden;
  margin: 0 auto;
  display: inline-block;
  position: relative;
  cursor: pointer;
}

.pic:before {
  width: 100%;
  height: 200px;
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
}

.pic:after {
  color: #fff;
  font-size: 18px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -20px -25px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  padding: 10px;
}

img {
  max-width: 100%;
  cusor: pointer;
}

.pic:hover img {
  animation: moveSlideshow 3s linear;
}

@keyframes moveSlideshow {
  100% {
    transform: translateY(-60%);
  }
}

.pic:hover .pic:after {
  opacity: 0;
}
<div class="pic"><img src="http://scr.templatemonster.com/51600/51651-big.jpg" alt="" /></div>


<div class="pic"><img src="http://www.cssauthor.com/wp-content/uploads/2014/06/Good-to-Go-single-page-PSD-template1.jpg" alt="" /></div>

回答1:


This works fine. I added a calc so that from the width of the image, the height of the div is minused and it scrolls to the bottom of the div only.

CSS:

.pic:hover img {
  animation: moveSlideshow 3s linear;
  animation-fill-mode: forwards;
}

JSFiddle: here




回答2:


You can use the animation-fill-mode: forwards; property. For the speed, you can control it with the animation-duration property (i.e. I adjusted it to 0'6 seconds in the code snippet).

body {
  margin: 2px auto;
  width: 500px;
}

.pic {
  width: 48%;
  height: 200px;
  overflow: hidden;
  margin: 0 auto;
  display: inline-block;
  position: relative;
  cursor: pointer;
}

.pic:before {
  width: 100%;
  height: 200px;
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
}

.pic:after {
  color: #fff;
  font-size: 18px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -20px -25px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  padding: 10px;
}

img {
  max-width: 100%;
  cusor: pointer;
}

.pic:hover img {
  animation: moveSlideshow .6s linear forwards;
}

@keyframes moveSlideshow {
  100% {
    transform: translateY(-60%);
  }
}

.pic:hover .pic:after {
  opacity: 0;
}
<div class="pic"><img src="http://scr.templatemonster.com/51600/51651-big.jpg" alt="" /></div>


<div class="pic"><img src="http://www.cssauthor.com/wp-content/uploads/2014/06/Good-to-Go-single-page-PSD-template1.jpg" alt="" /></div>


来源:https://stackoverflow.com/questions/45778802/how-to-scroll-down-when-hover-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!