当用户滚动到网页的特定部分时,如何使用javascript显示提醒。我试图通过检查document.body.clientWidth = document.documentElement.clientWidth
特定值并显示警报来显示相同内容。但它没有用。请帮帮我。
答案 0 :(得分:2)
请尝试以下代码:
var alertHeight = 500;
var alerted = false;
window.onscroll = function() {
if (window.scrollHeight >= alertHeight && !alerted) {
alert('Portion Reached for first time!');
alerted = true;
}
}
这将在用户滚动超过500px标记时提醒用户,并且仅在第一次提醒他们时。
答案 1 :(得分:0)
您应该使用==
运算符进行检查。将您的代码更改为:
document.body.clientWidth == document.documentElement.clientWidth
但是你的代码不会设置任何东西,因为它是赋值运算符,所以它不起作用。