admin管理员组文章数量:1414908
I want to alert something whenever my window get scrolled but it's not working. Here is my code
$(window).scroll(function(){
var ws = $(window).scrollTop();
if(ws)
{
alert('Scrolled');
}
});
I want to alert something whenever my window get scrolled but it's not working. Here is my code
$(window).scroll(function(){
var ws = $(window).scrollTop();
if(ws)
{
alert('Scrolled');
}
});
Share
Improve this question
edited Jun 25, 2016 at 14:07
Mohammad
21.5k16 gold badges57 silver badges85 bronze badges
asked Jun 25, 2016 at 14:06
mukul sonimukul soni
1272 silver badges7 bronze badges
2
- It work, See this – Mohammad Commented Jun 25, 2016 at 14:12
- it works just add <div style="height:1500px"></div> – Maksym Semenykhin Commented Jun 25, 2016 at 14:29
4 Answers
Reset to default 2In my case, this rule was to blame:
html,
body { overflow-x: hidden; }
Hiding overflowing elements is fine as long as the rule is applied only to the body element.
Your code works fine if you add an HTML element having height
$(window).scroll(function(){
var ws = $(window).scrollTop();
if(ws)
{
console.log(ws);
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="height:1500px"></div>
Try this,
CSS
body{
height:1200px;
}
Jquery
$(window).on('scroll',function(){
alert('Hi');
});
(or)
$(window).on('scroll',function(){
var wstp = $(window).scrollTop();
if(wstp)
{
alert('hi');
}
});
Just add height to your body tag and scroll.
If you want to execute or initialize function, you have to write below simple code.
$(window).on("scroll", function() {
var scrollTop = $(window).scrollTop();
if(scrollTop >= 10) {
//here is your function name
}
});
本文标签: javascript(window)scroll(function) is not workingStack Overflow
版权声明:本文标题:javascript - $(window).scroll(function) is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745164026a2645586.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论