元素在scroll()上的位置已更改

changed position of element on scroll()

本文关键字:位置 scroll 元素      更新时间:2024-03-31

拥有以下小提琴:http://jsfiddle.net/hpXL4/202/

试图让它发挥作用,但似乎做不到?希望在滚动X高度后基本上更改元素的位置。

var cta = $("#bottomcta");
cta.on("scroll", function(e) { 
  if (this.scrollTop > 50) {
    cta.addClass("fixed");
  } 
    else {
    cta.removeClass("fixed");
  }
});

必须使用Windows元素。下面的代表性链接……:)

链接

var cta = $(window);
cta.on("scroll", function(e) { 
  if (cta.scrollTop() > 50) {
    $("#bottomcta").addClass("fixed");
  } 
    else {
    $("#bottomcta").removeClass("fixed");
  }
});

在代码中,每次调用滚动函数时都要添加或删除该类。这可能导致添加或删除类"固定"数百次。我不知道这是否能解决你的问题,但我会使用

cta.hasClass("fixed");

在尝试添加元素之前,检查该类是否已应用于该元素。