到达屏幕上的某个点后停止div

时间:2018-10-23 10:15:18

标签: javascript jquery html css

我正在尝试使div“框”停止在屏幕上移动并停留在到达div“横幅”末尾的位置:

那怎么可能?如果您注意到我的代码中有任何错误,也将不胜感激。

$(function(){
  $('.banner').hide().slideDown(800);
  $('.banner h1').hide().show().addClass('animateText')
  $('.box').hide()
  var $win = $(window);

  $win.on("scroll", function () {
    var top = $win.scrollTop();
    $('.banner h1').css({'transform': 'rotate(' + top + 'deg)'})

    if (top > 150){
      $('.banner h1').css({'opacity':0})
      $('.banner p').slideDown(800);
      $('.banner h2').css({'left': top*1.2 + 'px'})
      $('.box').slideDown(600)
      $('.box').css({'transform': 'translateY(' + top/2 + 'px)'})
      
    } else if(top < 400){
      $('.banner h1').css({'opacity':1})
      $('.banner p').slideUp(800);
      $('.banner h2').css({'left': '-400px', 'transition':'600ms'})
      $('.box').slideUp(1000);
      
    } 
    
    //kinda like this:
    if(top>$('.banner').outerHeight()){
      $('.box').stopRightWhereYouAre()
    } 
  });
});
body{
  margin:0;
  padding: 0;
  font-family: arial;
}
.banner{
  background: linear-gradient(to left, black 50%, white 50%);
  height: 100vh;
}

.banner h1{
  text-align: center;
  font-size: 300px;
  padding-top: 250px;
  margin: 0;
  font-weight: bold;
  background: linear-gradient(to right, black 50%, white 50%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.animateText{
  animation: slide 2000ms ease-in-out;
}

@keyframes slide{
  0%{
    margin-left: -800px
  }
  50%{
    font-size: 350px;
  }
  100%{
    font-size: 300px;
    text-align: center;
  }
}

.banner p{
  color:white;
  width: 450px;
  margin-left: 60%;
  margin-top: -17%;
  font-size: 1.5em;
  display: none;
}

h2{
  font-weight: bold;
  font-size: 100px;
  color:black;
  top:250px;
  left:-400px;
  position: absolute;

}

.box{
  background-color: black;
  height: 60vh;
  width: 40%;
  margin-top: -400px;
  padding-top: 100px;
}

.h{
  margin-bottom: 300vh
}

.box p{
  color:white;
  width: 400px;
  font-size: 1.5em;
  margin: 0;
  margin-left: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="banner">
  <h1>HELLO</h1>
  <div class="rotate"><h2>DESIGN</h2></div>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="box">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="h"></div>

1 个答案:

答案 0 :(得分:1)

$(function(){
  $('.banner').hide().slideDown(800);
  $('.banner h1').hide().show().addClass('animateText')
  $('.box').hide()
  var $win = $(window);

  $win.on("scroll", function () {
    var top = $win.scrollTop();
    $('.banner h1').css({'transform': 'rotate(' + top + 'deg)'})

    if (top > 150 && top<$('.banner').outerHeight()){
      $('.banner h1').css({'opacity':0})
      $('.banner p').slideDown(800);
      var lf = top*1.2 < (window.innerWidth - $('.banner')[0].offsetWidth) ? top*1.2 : (window.innerWidth - $('.banner')[0].offsetWidth);
      $('.banner h2').css({'left': lf + 'px'})
      $('.box').slideDown(600);
      var ty = ($('.banner')[0].offsetHeight - top*1.2) <= 0 ? 0 : ($('.banner')[0].offsetHeight - top*1.2)
      $('.box').css({'transform': 'translateY(' + ty + 'px)'})
      
    } else if(top < 400 && top<$('.banner').outerHeight()){
      $('.banner h1').css({'opacity':1})
      $('.banner p').slideUp(800);
      $('.banner h2').css({'left': '-400px', 'transition':'600ms'})
      $('.box').slideUp(1000);
      
    } 

  });
});
body{
  margin:0;
  padding: 0;
  font-family: arial;
}
.banner{
  background: linear-gradient(to left, black 50%, white 50%);
  height: 100vh;
}

.banner h1{
  text-align: center;
  font-size: 300px;
  padding-top: 250px;
  margin: 0;
  font-weight: bold;
  background: linear-gradient(to right, black 50%, white 50%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.animateText{
  animation: slide 2000ms ease-in-out;
}

@keyframes slide{
  0%{
    margin-left: -800px
  }
  50%{
    font-size: 350px;
  }
  100%{
    font-size: 300px;
    text-align: center;
  }
}

.banner p{
  color:white;
  width: 450px;
  margin-left: 60%;
  margin-top: -17%;
  font-size: 1.5em;
  display: none;
}

h2{
  font-weight: bold;
  font-size: 100px;
  color:black;
  top:250px;
  left:-400px;
  position: absolute;

}

.box{
  background-color: black;
  height: 60vh;
  width: 40%;
  /*margin-top: -400px;*/
  padding-top: 100px;
}

.h{
  margin-bottom: 300vh
}

.box p{
  color:white;
  width: 400px;
  font-size: 1.5em;
  margin: 0;
  margin-left: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="banner">
  <h1>HELLO</h1>
  <div class="rotate"><h2>DESIGN</h2></div>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="box">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="h"></div>