![](/img/trans.png)
[英]How to put an element in hover state when scrolled down using javascript
[英]Run javascript when scrolled down
我无法使我的JavaScript正常工作。 它应该在“用户”向下滚动2像素但没有任何反应时运行。 当我删除Scroll函数时,一切正常,所以我写错了吗?是的,为什么它不起作用?
HTML
<div id="report"></div>
<div style="position:relative;">
<img id="myImage" src="test.svg" style="margin-left:200px; width:100px;" />
</div>
使用Javascript
(window).scroll(function() {
if($(window).scrollTop() > 2) {
var animate, left=0, imgObj=null, report = document.getElementById('report'), i=0;
function init(){
imgObj = document.getElementById('myImage');
imgObj.style.position= 'absolute';
imgObj.style.top = '240px';
imgObj.style.left = '-300px';
imgObj.style.visibility='hidden';
moveRight();
}
function moveRight(){
left = parseInt(imgObj.style.left, 10);
if (10 >= left) {
imgObj.style.left = (left + 5) + 'px';
imgObj.style.visibility='visible';
animate = setTimeout(function(){moveRight();},20); // call moveRight in 20msec
//stopanimate = setTimeout(moveRight,20);
} else {
stop();
}
//f();
}
function stop(){
clearTimeout(animate);
}
// starting
window.onload = function() {init();};
}
});
这是小提琴,并且有效!
http://jsfiddle.net/df6g497p/6/
// you forgot the $ <--- this is all i changed
$(window).scroll(function() {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.