首页 > 解决方案 > 在特定点而不是像素处滚动添加删除类

问题描述

我需要在页面上的特定点删除和添加一个类,但该点在每个页面上都不同,因此“if (scroll >= 300)”不适用于所有页面。有没有办法基于滚动到部分 id 来做到这一点?

$(function() {
//caches a jQuery object containing the content element
var content = $(".isi-fixed");
$(window).scroll(function() {
    var scroll = $(window).scrollTop();

    if (scroll >= 300) {
        content.removeClass('isi-fixed').addClass("isi");
    } else {
        content.removeClass("isi").addClass('isi-fixed');
    }
});

我的CSS是

.isi-fixed {
width: 100%;
height: 20%;
bottom: 0px;
right: 0px;
padding: 50px;
opacity: 0.5;
overflow-y: scroll;
visibility: visible;
display: block;
z-index: 500 !important;
position: fixed;    
background-color: #E7E7E7;
 }

.isi {
width: 100%;
padding: 50px;
visibility: visible;
position: relative;
background-color: #E7E7E7;
 }

标签: javascript

解决方案


推荐阅读