当html,body设置为height:100%时如何scrollTop? [英] How to scrollTop when html, body is set to height: 100%?

查看:221
本文介绍了当html,body设置为height:100%时如何scrollTop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非我从body和html中删除 height:100%,否则以下情况不起作用。但是,我需要这种风格,因为我将它用于页面上的其他元素。

The following won't work unless I remove height: 100% from body and html. However, i need that style as I am using it for other elements on the page.

html

<a href="#" id="scrollTop">Back to top</a>

jQuery

$("#scrollTop").on("click",function(e){
  e.preventDefault();
  $(window).animate({scrollTop: 0}, 'slow');
});

即使尝试以下结果仍有负面结果

Even tried the following with still negative results

$("#scrollTop").on("click",function(e){
  e.preventDefault();
  $("body, html").animate({scrollTop: 0}, 'slow');
});

Css

html, body {height:100%; min-height:100%;}


推荐答案

遇到问题,感谢评论,我注意到我有正文:overflow-x:隐藏这意味着当我们在body上使用overflow时,scrollTop无效,html

Got the issue, thanks to comments I noticed I had body: overflow-x:hidden which means scrollTop isn't working when we are using overflow on body, html

这篇关于当html,body设置为height:100%时如何scrollTop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆