2
关注
1381
浏览

滚动到div的ID,堆叠后“停止” DIV ID隐藏(不smoth滚动)

为什么被折叠? 0 个回复被折叠
zhang465610921 未验证用户 用户来自于: 广东省
2020-09-16 08:57

所以,你要当另一个特定的div是在它被隐藏视图。那么,你必须知道该div的位置,并使用该数字来调整你的滚动比较。

所以,你必须采取3次测量:

  1. 用户屏幕高度
  2. 的顶部位置的“停格”的
  3. 底部位置的“停格”

然后,一些简单的数学......并与滚动的位置进行比较。

$(document).ready(function(){ 
 
    // Get some measurements 
 
    var stopPosition = $("#stop").offset().top; 
 
    var stopHeight = $("#stop").outerHeight(); 
 
    var displayHeight = $(window).height(); 
 

 

 
    // Scroll handler 
 
    $(window).scroll(function() { 
 

 
    // Show the fixed black image when the stop div is in view 
 
    if($(this).scrollTop() < stopPosition-displayHeight || $(this).scrollTop() > stopPosition+stopHeight){ 
 
     $('#goSale').stop().animate({ 
 
     top: '65px' 
 
     }, 1); 
 

 
     // Else, hide it. 
 
    }else{ 
 
     $('#goSale').stop().animate({ 
 
     top: '-1000px' 
 
     }, 1); 
 
    } 
 
    }); 
 
});
#a,#b,#c{ 
 
    height:1000px; 
 
} 
 
#a{ 
 
    background-color:blue; 
 
} 
 
#b{ 
 
    background-color:orange; 
 
} 
 
#c{ 
 
    background-color:green; 
 
} 
 
#stop{ 
 
    height:300px; 
 
    border:10px solid red; 
 
} 
 
#goSale{ 
 
    position:fixed; 
 
    top:65px; 
 
    right:20px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">script> 
 

 
<div id="a">div> 
 
<div id="stop"> 
 
    
 
    <h1>The phone number in the black image is not shown when I'm in view.h1> 
 
    
 
div> 
 
<div id="b">div> 
 
<div id="c">div> 
 
<img id="goSale" src="http://www.vichy.ho.ua/img/pages/sale.png">

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 15:53
关注人数
2 人关注
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号