div底部滚动到浏览器窗口底部以上时添加属性

时间:2017-05-29 19:21:07

标签: javascript jquery css

当其他div(id="text1")滚动到浏览器窗口底部上方时,我希望将某个属性添加到某个div(id="one")。当"一个"的底部再次滚动浏览器窗口底部边缘下方,应再次删除该属性。

这是我想要添加和删除的属性:

data-scroll-speed="2"

See example here

1 个答案:

答案 0 :(得分:1)

第一:您需要包含jquery

第二次:您需要使用窗口public ActionResult Post([FromForm]int id) 事件

第3次:您需要获得scroll #two.offset().top

参见演示

outerHeight()
$(document).ready(function(){
  $(window).on('scroll' , function(){
    var WindowScrollTop = $(this).scrollTop(),
        Div_one_top = $('#one').offset().top,
        Div_one_height = $('#one').outerHeight(true),
        Window_height = $(this).outerHeight(true);
    if(WindowScrollTop >= (Div_one_top +  Div_one_height) - WindowScrollTop){
       $('#one').css('background' , 'red');
       $('#text1').removeAttr('data-scroll-speed');
    }else{
       $('#one').css('background' , 'black');
       $('#text1').attr('data-scroll-speed', '2');
    }
  }).scroll();
});
#one {
    height: 120vw;
    width: 100%;
    top: 0px;
    background-color: pink;
}

#text1 {
    width: 100%;
    font-size: 9em;
    margin-top: 100vw;
    position: absolute;
	  color:white;
}

#two {
    height: 50vw;
    width: 100%;
    top: 0px;
    background-color: green;
}

相关问题