javascript - 页面滚动到 div 的底部

标签 javascript html css scroll window.location

如何在单击按钮时将 div 底部与页面底部对齐?类似于使用 window.location.href = '#Div1' 将屏幕对齐到 'Div1'

的顶部
   <html>
<head>
</head>
<body>
    <div id="Div1" style="border: 1px solid #cccccc; position: absolute; background: #FA8258"
        onclick="window.location.href='#Div2'">
        This is Div Orange. Click to see Div Yellow.
    </div>
    <div id="Div2" style="border: 1px solid #cccccc; position: absolute; top: 800px;
        background: #F4FA58" onclick="window.location.href='#Div3'">
        This is Div Yellow. Click to see Div Blue. But I want this div to be at the bottom
        instead of top on click of Div Orange.
    </div>
    <div id="Div3" style="border: 1px solid #cccccc; position: absolute; top: 1600px;
        background: #A9F5E1;" onclick="window.location.href='#Div1'">
        This is Div Blue. Click to see Div Orange.
    </div>
</body>
</html>

最佳答案

我认为这会为您完成工作。这是假设您想要在正文中滚动。 $(window).height() 指视口(viewport)高度。

function jumpBottom(from, id) {
    var to = document.getElementById(id);
    document.body.scrollTop = to.offsetTop - $(window).height() + to.offsetHeight;
}
<div id="Div1" style="border: 1px solid #cccccc; position: absolute; background: #FA8258"
        onclick="jumpBottom(this,'Div2')">
        This is Div Orange. Click to see Div yellow.
    </div>
    <div id="Div2" style="border: 1px solid #cccccc; position: absolute; top: 800px;
        background: #F4FA58" onclick="window.location.href='#Div3'">
        This is Div Yellow. Click to see Div Blue. But I want this div to be at the bottom
        instead of top on click of Div Orange.
    </div>
    <div id="Div3" style="border: 1px solid #cccccc; position: absolute; top: 1600px;
        background: #A9F5E1;" onclick="window.location.href='#Div1'">
        This is Div Blue. Click to see Div Orange.
    </div>

关于javascript - 页面滚动到 div 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31178393/

相关文章:

javascript - 将 .CSV 转换为 HTML 表

html - 使移动客户端的网页变薄

javascript - 如何在 ES6 Map 中中断/中断/停止 forEach?

javascript - 使用 FancyBox 出现 jQuery 未定义错误

javascript - 为什么我无法在 iOS 上禁用输入或按钮元素?

html - 将样式应用于具有特定值的孙子的祖 parent

css - 之前带有 css 的透明预加载器 (sass)

html - 如何让我的导航栏品牌响应低分辨率?

javascript - 如何在 lodash.template() 中取消转义 html 标签?

javascript - 我需要从 jQuery 的最后一个元素中提取第二个的 ID