【问题标题】:How to add Position Fixed on specific Div and then remove that class on scroll如何在特定 Div 上添加 Position Fixed,然后在滚动时删除该类
【发布时间】:2019-10-31 08:13:31
【问题描述】:

这是我正在开发的 Jsfiddle https://jsfiddle.net/farooqshad/jbdczk10/10/

基本上我想在滚动到特定 div 时添加一个类,然后删除该类。

这是我的 javascript

var YourDiv = $(".mainwrapper");
$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  console.log(scroll);
  if (scroll >= YourDiv.offset().top - 10) {
    YourDiv.addClass('fixed');
    console.log("fixed");
  } else {
    YourDiv.removeClass('fixed');
    console.log("Not Fixed");
  }
});

【问题讨论】:

  • 什么不起作用?当我运行您的 jsfiddle 时,它​​会添加该类并再次将其删除
  • @Sofyan Thayf 在那个 mainwrapper div 之后我想删除其他条件不起作用的类

标签: javascript jquery css scroll fixed


【解决方案1】:

farooq 试试这个解决方案

var YourDiv = $(".mainwrapper");
var foo=$(".footer1")
$(window).scroll(function () {
var scroll = $(window).scrollTop();

if (scroll >= YourDiv.offset().top - 10 && scroll<=foo.offset().top - 10) {
    YourDiv.addClass('fixed');
}
else
{
    YourDiv.removeClass('fixed');
}
});

如果不起作用,请告诉我 这是fiddle

【讨论】:

  • 感谢它的工作,但如果页面刷新和滚动在该部分上,我还需要添加一个类
猜你喜欢
  • 2018-11-09
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多