window.scrollTop 未定义
window.scrollTop is undfined
为什么 window.scrollTop
未定义?
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop
The Element.scrollTop property gets or sets the number of pixels that
an element's content is scrolled vertically.
An element's scrollTop value is a measurement of the distance from the
element's top to its topmost visible content. When an element's
content does not generate a vertical scrollbar, then its scrollTop
value is 0.
jQuery(window).on('scroll', function() {
if(window.scrollTop > 0) {
console.log('of the top')
console.log('scroll top value is: ' + window.scrollTop)
} else {
console.log('the top')
console.log('scroll top value is: ' + window.scrollTop)
}
})
我在控制台日志中得到的是:
scroll top value is: undefined
而且我总是在 if else 中得到一个评估:
of the top
即使我滚动到页面顶部。
为什么 window.scrollTop 未定义?
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop window 不算元素吗?
您需要使用 jQuery $ 标识符以及 .scrollTop() 的括号 () 才能获得位置
这是可执行代码:
<script>
jQuery(window).on('scroll', function() {
if($(window).scrollTop() > 0) {
console.log('of the top')
console.log('scroll top value is: ' + $(window).scrollTop())
} else {
console.log('the top')
console.log('scroll top value is: ' + $(window).scrollTop())
}
})
</script>
<script>
jQuery(window).on('scroll', function() {
if($(window).scrollTop() > 0) {
console.log('of the top')
console.log('scroll top value is: ' + $(window).scrollTop())
} else {
console.log('the top')
console.log('scroll top value is: ' + $(window).scrollTop())
}
})
</script>
YESSINE 的代码是正确的。但可以让它变得简单
<script>
jQuery(window).on('scroll', function() {
var window = $(window);
if(window.scrollTop() > 0) {
console.log('of the top')
console.log('scroll top value is: ' + window.scrollTop())
} else {
console.log('the top')
console.log('scroll top value is: ' + window.scrollTop())
}
})
</script>
为什么 window.scrollTop
未定义?
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop
The Element.scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically.
An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0.
jQuery(window).on('scroll', function() {
if(window.scrollTop > 0) {
console.log('of the top')
console.log('scroll top value is: ' + window.scrollTop)
} else {
console.log('the top')
console.log('scroll top value is: ' + window.scrollTop)
}
})
我在控制台日志中得到的是:
scroll top value is: undefined
而且我总是在 if else 中得到一个评估:
of the top
即使我滚动到页面顶部。
为什么 window.scrollTop 未定义?
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop window 不算元素吗?
您需要使用 jQuery $ 标识符以及 .scrollTop() 的括号 () 才能获得位置
这是可执行代码:
<script>
jQuery(window).on('scroll', function() {
if($(window).scrollTop() > 0) {
console.log('of the top')
console.log('scroll top value is: ' + $(window).scrollTop())
} else {
console.log('the top')
console.log('scroll top value is: ' + $(window).scrollTop())
}
})
</script>
<script> jQuery(window).on('scroll', function() { if($(window).scrollTop() > 0) { console.log('of the top') console.log('scroll top value is: ' + $(window).scrollTop()) } else { console.log('the top') console.log('scroll top value is: ' + $(window).scrollTop()) } }) </script>
YESSINE 的代码是正确的。但可以让它变得简单
<script>
jQuery(window).on('scroll', function() {
var window = $(window);
if(window.scrollTop() > 0) {
console.log('of the top')
console.log('scroll top value is: ' + window.scrollTop())
} else {
console.log('the top')
console.log('scroll top value is: ' + window.scrollTop())
}
})
</script>