简体   繁体   English

如何将div放在页面顶部

[英]How to stick div on top of the page

I just want to stick the div on top of the page 我只想将div放在页面顶部

if someone scrolls the page then the green div stickdiv should be automatically stick in the top 如果有人滚动页面,则绿色div stickdiv应该自动stickdiv在顶部

var left = document.getElementsByClassName("stickdiv");

for( var i = 0;i<left.length;i++){
var stop = (left[0].offsetTop);

window.onscroll = function (e) {
var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
    if (scrollTop >= stop) {
     left.className += " stick"; //adding a class name
    } else {
        className = '';
    }

}
}

why adding stick class on the div does not working - https://jsfiddle.net/kzk5qab2/1/ 为什么在div上添加Stick类不起作用-https: //jsfiddle.net/kzk5qab2/1/

I just want to stick the div at top like yellow div on this - http://jsfiddle.net/qc4NR/ 我只想将div像黄色的div一样放在顶部-http: //jsfiddle.net/qc4NR/

You have looped into the array of left items, but forget to reference the array index when trying to add the class name to the element. 您已经遍历了left项的数组,但是在尝试将类名称添加到元素时忘记了引用数组索引。

 var left = document.getElementsByClassName("stickdiv"); for (var i = 0; i < left.length; i++) { var stop = (left[0].offsetTop); window.onscroll = function(e) { var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop; // left.offsetTop; if (scrollTop >= stop) { // get array item by index left[0].classList.add('stick'); //adding a class name } else { // get array item by index left[0].classList.remove('stick'); } } } 
 .stickdiv { height: 50vh!important; width: 100vh!important; background-color: green!important; } .stick { position: fixed; top: 0; margin: 0 0 } #right { float: right; width: 100px; height: 1000px; background: red; } .des { height: 300px; width: 100%; background-color: #000; } 
 <div class="des"></div> <div class="stickdiv"></div> <div id="right"></div> 

You don't even need to loop if you select the item directly, as in the following: 如果直接选择项目,甚至不需要循环,如下所示:

var left = document.querySelector(".stickdiv");
var stop = (left.offsetTop);

window.onscroll = function(e) {
  var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
  // left.offsetTop;
  if (scrollTop >= stop) {
    left.classList.add('stick'); //adding a class name
  } else {
    left.classList.remove('stick');
  }
}

Depending on what browsers you need to support, you don't necessarily need JS for this. 根据您需要支持的浏览器,您不一定需要使用JS。

.sticky {
    position: sticky;
    left: 0;
    top: 0;
    width: 100%;
}

Codepen: https://codepen.io/afisher88/pen/xJaxoP Codepen: https ://codepen.io/afisher88/pen/xJaxoP

Browser support for position sticky: https://caniuse.com/#search=position%3A%20sticky 浏览器对位置粘性的支持: https : //caniuse.com/#search=position%3A%20sticky

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM