jquery - 如何让DIV自动滚动适应屏幕

标签 jquery html css

我有 2 个 DIV(1 和 2)都是 100% 宽度、100% 高度和position:absolute,我想要实现的是当用户查看 DIV 1 并向下滚动约 10% 时,DIV 2自动滚动以适合屏幕。

这是示例代码 https://jsfiddle.net/d6bpr5o2/

<div id="one" class="content">
when user scroll like 10% down. the screen automatically scroll to the next div.
</div>

<div id="two" class="content">
this is second screen
</div>

这是 CSS:

.content{
    position:absolute;
    width:100%;
    height:100%;
    left:0;
    top:0;
}

#one{
    background:blue;
}

#two{
    top:100%;
    background:rgba(255,255,0,1.00);
}

enter image description here

最佳答案

也许我的代码可以帮到您? https://jsfiddle.net/moongod101/zbyy7mc5/

$(function(){
    $div1 = $('.div1')
  $div2 = $('.div2')
    $div2Pos = $('.div2').offset().top;
  $time = 0
    $(window).scroll(function(){

    $scrollPos = $(this).scrollTop();
    $to = $div2Pos = 100

    if($scrollPos >= $to){

      if($time == 0){
        //Not scroll before
        $('html, body').animate({
          scrollTop: $div2.offset().top
            }, 2000);
        $time ++
      }

    }


  });

});

关于jquery - 如何让DIV自动滚动适应屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475161/

相关文章:

javascript - 使用 append() 将页面滚动到底部时如何显示数据;

jquery - 未定义不是函数(jQuery 添加/删除类)

html - 如何防止固定元素滚动到另一个元素上方或下方?

html - minmax() 默认为最大值

css - 在 CSS id 或类中使用 camelCase 是否可以?

css - 背景附件 : fixed not working as expected in firefox

javascript - 克隆 jquery 按钮两次并让它们都在执行相同的任务

jquery显示/隐藏div

javascript - 使用 javascript/jQuery 设置文本输入值时未反射(reflect)更改

html - 独立于 <select> 调整 <option> 的大小