动画滚动jquery不工作
Animate scroll jquery not working
所以我想我可能已经知道我的问题是什么了——为了让我的网站响应,我使用了height:auto
,并使用min-height:some-percentage
设置了高度。我知道这可能不是最佳实践,但它很有效,而且比媒体查询花费的时间要少得多。
因此,当我没有使用上述方法设置高度时,下面的动画滚动代码就可以工作了:
$('.animate_scroll').click(function(e){
e.preventDefault(); //I tried without this line
var loc_id = $($(this).attr('href')),
loc_pos = loc_id.offset().top;
console.log($(this).attr('href')); //outputs the right div id
console.log(loc_pos); //outputs offset greater than 0
$('html, body').animate({scrollTop : loc_pos}, 300)
})
有人知道我做错了什么吗?我之所以说我想我已经知道我的问题是什么了,是因为上面的console.log
语句记录了有效值。
更新:在上面的代码中,我又添加了几个console.log语句来获取偏移量值。值是不同的,但即使我将loc_id设置为document.getElementById(this.getAttribute('id')).offsetTop
,然后将其用作scrollTop值,它也不会滚动。
console.log($(this).attr('href')); //correct id
console.log(loc_pos); //outputs 794.546875
console.log(document.getElementById(this.getAttribute('id')).offsetTop); //944
尝试这个演示
$(".jumper").on("click", function( e ) {
e.preventDefault();
$("body, html").animate({
scrollTop: $( $(this).attr('href') ).offset().top
}, 2000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="pliip">pliip</div><a class="jumper" href="#ploop">Ploop</a><a id="#a_id">LINK TO MOVE</a>
<div style="height:1000px;background-color:#666">div</div>
<!-- Landing elements -->
<a class="jumper" href="#pliip">Pliip</a>
<div id="ploop">ploop</div>
相关文章:
- 不正确的工作jquery插件bxslider与插件elevatezoom
- 更改图像src-dons't工作jquery/js
- event.prventDefault()不工作(jQuery实时)
- delay()不会'第二次工作jquery
- 如何工作 jQuery 就绪
- 几天后,昨天停止工作jQuery
- .click() 不再工作 jquery.
- 如何使工作jquery"“滑动”;在iPad上滚动
- 为什么不是't这个选择器正在工作(Jquery和Asp.net)
- Flexslider无法在html网站中工作(jQuery无法加载?)
- 点击,点击不工作?jquery
- 按钮在“加”和“减”之间切换,需要“点击”;只能在fa +上工作.jQuery
- 突出显示搜索列表不工作(jquery问题)
- 页眉内的Div /靠近页面顶部/在2次点击事件后不显示,而它的's页脚双胞胎工作- jquery
- 为什么css后台jQuery代码不工作jQuery设置多个css值
- .stop()不是't使用我的悬停,使快速移动工作-jQuery
- 自定义验证方法不工作- jQuery验证插件
- 数据表columnDefs不工作:jquery / javascript
- 工作jQuery代码赢得'不要在电话间隙跑步
- 我的Javascript代码不工作- Jquery选择器问题