如何在第一次使用javascript滚动页面时提醒?
我只想提醒第一个滚动页面,我该怎么做?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(window).scroll(function () {
var xxx;
if(xxx === '')
{
var xxx = "test";
var div = $("#myDiv");
alert(div.height());
}
});
</script>
<DIV id="myDiv" style="height:auto; width:78;overflow:hidden"> Simple Test</DIV>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
答案 0 :(得分:2)
使用.one()
将处理程序附加到元素的事件。处理程序已执行 每个事件类型每个元素最多一次。
$(window).one("scroll", function() {
console.log("foo");
});
答案 1 :(得分:1)
将变量作为全局变量并使用布尔值而不是字符串,这在使用代码标志时更好。除此之外,在滚动函数中设置变量时无需重新定义变量。
var xxx;
$(window).scroll(function () {
if(!xxx)
{
xxx = true;
var div = $("#myDiv");
alert(div.height());
}
});
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<DIV id="myDiv" style="height:auto; width:78;overflow:hidden"> Simple Test</DIV>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
&#13;
答案 2 :(得分:0)
onscroll = function () {
var xxx = "test";
var div = $("#myDiv");
alert(div.height());
onscroll = null; // remove function so wont get called next time
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<DIV id="myDiv" style="height:auto; width:78;overflow:hidden"> Simple Test</DIV>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
答案 3 :(得分:0)
此代码仅在用户在页面上进行第一次滚动时发出警报。
var scrolled = false;
$(window).scroll(function() { if (scrolled == false) { alert("You Have Just Scrolled"); scrolled = true; } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>
<div>This is some text</div>