javascript - 当用户滚动时,我希望 div 在页面底部滚动

标签 javascript jquery html css

我不能为此使用 bootstrap 我可以使用 jquery

我有一个带有按钮和一些段落的容器的基本页面 我想在用户滚动时将其移动到页面底部

此外,这应该只发生在移动 View 中。

示例:https://creditcards.chase.com/a1/marriottpremier/8TK?CELL=679Z&nck=120931994&ck=1213078&lk=1000160171

他们使用了 bootstrap

<div>
<h2>Near<h2>
<div>
<button>Learn more<button>
<p>conditions<p>
<div>

最佳答案

我已经使用 jquery 和媒体查询来实现这一点。

CSS
@media only screen and (max-width: 380px) {


    .fixed {
        bottom: 0;
        position: fixed;
        left: 0;
        width: 100%;
        max-width: 374px;
        margin: 0 auto;
        background-color: blue;
        height: 70px;

        }
    }


JQuery

<script>
jQuery(document).ready(function() {

// define variables
var navOffset, scrollPos = 0;

// add utility wrapper elements for positioning
jQuery("nav").wrap('<div class="button1"></div>');
jQuery("nav").wrapInner('<div class="inner"></div>');
jQuery(".nav-inner").wrapInner('<div class="inner-most"></div>');

// function to run on page load and window resize
function stickyUtility() {

// only update navOffset if it is not currently using fixed position
if (!jQuery("nav").hasClass("fixed")) {
navOffset = jQuery("nav").offset().top;
}

// apply matching height to nav wrapper div to avoid awkward content jumps
jQuery(".nav-placeholder").height(jQuery("nav").outerHeight());

} // end stickyUtility function

// run on page load
stickyUtility();

// run on window resize
jQuery(window).resize(function() {
stickyUtility();
});

// run on scroll event
jQuery(window).scroll(function() {

scrollPos = jQuery(window).scrollTop();

if (scrollPos >= navOffset) {
jQuery("nav").addClass("fixed");
} else {
jQuery("nav").removeClass("fixed");
}

});

});
</script>

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

相关文章:

javascript - 如何使用javascript删除div中的特定元素?

javascript - 如何修复,ENOENT 错误 : no such file or directory when the "fonts" task mentioned below in the gulpfile. js 启动

javascript - 有没有在 Firefox 浏览器中检测 Windows-11 和 Windows-10 的方法?

javascript - 对于可以多次加载的ajax页面,如何处理只加载一次js

javascript - 使用组的 jQuery 对象选择比 $($ (".someclass")[i])) 更简单的方法?

javascript - 提交按钮无法使用 JavaScript 验证表单

html - 简单的 z-index 挑战 : making a sibling disappear behind another sibling?

javascript - Redux Action 中的轮询

jquery - 使用 jQuery 选择器匹配第一个匹配的分层后代

html - ionic : how to change the size of FAB icon