jQuery和Javascript:offset.top()没有响应

jQuery and Javascript: offset.top() not responsive

本文关键字:响应 top Javascript offset jQuery      更新时间:2024-01-01

有没有办法使offset.top()响应?如果没有,当滚动上的动画div接触到浏览器顶部时,我如何使其响应?

在我的小提琴里,当你向下滚动时,div会正确地到达顶部,但如果你把屏幕按高度挤压,它就不会再按预期工作了
据我所知,这是因为存储div的变量被调用了一次,但当我将其绑定到滚动事件时,div并没有按预期工作。

小提琴:https://jsfiddle.net/jzhang172/j2yrumyg/8/

$(function(){
var cheese= $('.ok').offset().top; //Define top of 'hey'
//
//Animates when it reaches red part of page
//
$(window).scroll(function() {
    if ( $(window).scrollTop() >= cheese  ) {
  
        $('.ok').addClass('top');
        $('.nice').hide().fadeIn().html('ok');
            $(".nice").animate({ 
            left:"0"
        }, 600);
        $('.nice').addClass('maybe');
    }
    else{
                $('.ok').removeClass('top');
                $('.nice').removeClass('maybe');
                $('.nice').html('Hey');
    }
});
//
//This part doesn't rely on scroll event and is in charge of changing hover on ".ok" div.
//
      $('.ok').hover(function(){
         if(!$(this).hasClass("top"))
           $(this).addClass('proj-hover');
         
              },function(){
                $(this).removeClass('proj-hover');
               
              });
//
//Animate on click
//
$('.ok').click(function(){
    if ( $(window).scrollTop() >= cheese  ) {
}
else{
    $("body, html").animate({ 
            scrollTop: $('.other').offset().top 
        }, 600);
     
}
});


});
*{
  box-sizing:border-box;
}
body,html{
  padding:0;
  margin:0;
}
.div{
  height:100vh;
  width:100%;
  background:#6464FF;
}
.other{
  height:1000px;
  width:100%;
  background:#FF6161;
}
.ok{
  position:absolute;
  bottom:0;
  left:50%;
  margin-left:-100px;
  width:200px;
  height:50px;
  line-height:50px;
  background:black;
  color:white;
  text-align:center;
  transition:1s;
}
.top{
  position:fixed;
  top:0;
  left:0;
  transition:.7s;
      margin-left: 0px;
      width:100%;
}
.proj-hover{
  background:white;
  color:black;
}
.blue{
  background:blue;
}
.nice{
  transition:0s;
    margin: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
  
  <div class="ok">
  <p class="nice">Hey</p>
  </div>
</div>
<div class="other">
  
</div>

您的代码刚开始时从元素.ok获取.top()。为了使其工作,您还应该使用不会更改其位置的东西,如.other元素。

在这里更新了您的代码:

$(window).scroll(function() {
    var cheese= $('.other').offset().top; //Define top of 'hey'
    if ( $(window).scrollTop() >= cheese  ) {
        ...
    }
});

更新
但是,如果你想根据元素.ok获得位置,你需要为它创建一个占位符元素。

HTML

<div class="div">
  <div class="placeholder">
    <div class="ok">
      <p class="nice">Hey</p>
    </div>
  </div>
</div>
<div class="other"></div>

CSS

.ok,
.placeholder{
  position:absolute;
  bottom:0;
  left:50%;
  margin-left:-100px;
  width:200px;
  height:50px;
  line-height:50px;
  background:black;
  color:white;
  text-align:center;
  transition:1s;
}

JS

$(window).scroll(function() {
    var cheese= $('.placeholder').offset().top; //Define top of 'hey'
    if ( $(window).scrollTop() >= cheese  ) {
        ...
    }
});

篡改结果