简体   繁体   English

如何根据图像的高度翻译图像?

[英]How do I translate an image based on its height?

I have a tall image, that's in a div that's not as tall. 我有一个很高的形象,那是在一个不那么高的div中。 I want to show the whole image by translating it. 我想通过翻译显示整个图像。 This is what I have so far: 这是我到目前为止的内容:

https://jsfiddle.net/swv0w0em/ https://jsfiddle.net/swv0w0em/

  document.addEventListener("mouseover", function(event) { var element = event.target; if (element.classList.contains("myImage") && element.naturalWidth < element.naturalHeight) { element.className += " imageScroll"; //element.style.transform = "translate(0,-" + element.naturalHeight + ")"; //element.style.mozTransition = "translate(0,-" + element.naturalHeight + ")"; //element.style.msTransform = "translate(0,-" + element.naturalHeight + ")"; //element.style.oTransform = "translate(0,-" + element.naturalHeight + ")"; //element.style.webkitTransform = "translate(0,-" + element.naturalHeight + ")"; } }); document.addEventListener("mouseout", function(event) { var element = event.target; if (element.classList.contains("myImage")) { element.className = element.className.replace( /(?:^|\\s)imageScroll(?!\\S)/g, '' ); } }); 
 div { height: 200px; overflow: hidden; background-color: red; } img { -moz-transition: all 3s ease-in-out; -o-transition: all 3s ease-in-out; -webkit-transition: all 3s ease-in-out; transition: all 3s ease-in-out; } .imageScroll { -moz-transform: translate(0, -350px); -ms-transform: translate(0, -350px); -o-transform: translate(0, -350px); -webkit-transform: translate(0, -350px); transform: translate(0, -350px); } 
 <div> <img src="https://placeimg.com/300/800/any" class="myImage" alt="SomeImage"> </div> 

However, I can't figure out how to translate with the image height. 但是,我不知道如何使用图像高度进行翻译。 I want the scrolling to stop as soon as the bottom of the image is in view. 我希望在图像底部可见时立即停止滚动。 I don't know how tall the image will be before, so I can't hardcode it in CSS. 我不知道图像的高度,因此无法在CSS中对其进行硬编码。 That why I tried setting the style in JavaScript, but that did not work for me (see the commented out code). 这就是为什么我尝试在JavaScript中设置样式的原因,但是这对我不起作用(请参阅注释掉的代码)。

My question: how do I get the image to scroll all the way down on mouseover, and stop scrolling when the bottom of the image is reached? 我的问题:如何在鼠标悬停时使图像一直向下滚动,并在到达图像底部时停止滚动? I can't scroll past the height either, or I'll get ugly white space. 我也不能滚动超过高度,否则我会得到难看的空白。 I'm trying to get this done in pure JavaScript, without jQuery . 我正在尝试在没有jQuery的情况下使用纯JavaScript完成此操作。 Who can help? 谁能帮忙?

With pure CSS you can use percentage unit in the translate, then subtract the height of container using top offset (position absolute): 使用纯CSS,您可以在转换中使用百分比单位,然后使用顶部偏移(绝对位置)减去容器的高度:

 div { height: 200px; overflow: hidden; position: relative; } .myImage { transition: all 3s ease-in-out; display: block; position: absolute; top: 0; left: 0; } .myImage:hover { top: 100%; transform: translateY(-100%); } 
 <div> <img src="https://placeimg.com/300/800/any" class="myImage" alt="SomeImage"> </div> 

jsFiddle: https://jsfiddle.net/azizn/h37dLzgf/1/ jsFiddle: https ://jsfiddle.net/azizn/h37dLzgf/1/

The commented out code has missing "px" 注释掉的代码缺少“ px”

 element.style.transform = "translate(0,-" + element.naturalHeight + "px)";
 element.style.mozTransition = "translate(0,-" + element.naturalHeight + "px)";
 element.style.msTransform = "translate(0,-" + element.naturalHeight + "px)";
 element.style.oTransform = "translate(0,-" + element.naturalHeight + "px)";
 element.style.webkitTransform = "translate(0,-" + element.naturalHeight + "px)";

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

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