gpt4 book ai didi

javascript - 向下滚动时运行 JavaScript

转载 作者:行者123 更新时间:2023-12-03 10:24:25 26 4
gpt4 key购买 nike

我无法让我的 JavaScript 工作。当“用户”向下滚动 2 像素但没有任何反应时,它应该运行。当我删除滚动功能时,一切正常,所以我写错了什么,或者是的,为什么它不起作用?

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();};
}
});

最佳答案

这是 fiddle ,它可以工作!

http://jsfiddle.net/df6g497p/6/

// you forgot the $ <--- this is all i changed 
$(window).scroll(function() {

关于javascript - 向下滚动时运行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com