简体   繁体   English

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

[英]changed position of element on scroll()

Have the following fiddle: http://jsfiddle.net/hpXL4/202/ 具有以下提琴: http : //jsfiddle.net/hpXL4/202/

Trying to get it to work, but can't seem to? 试图使其正常工作,但似乎不能吗? Looking to basically change the position of the element after scrolling X height. 滚动X高度后,希望基本更改元素的位置。

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

In your code you are adding or removing a the class every time the scroll function in called. 在代码中,每次调用scroll函数时,都在添加或删除类。 This could result in adding or removing the class "fixed" hundred of times. 这可能导致数百次添加或删除“固定”类。 I don't know if this will fix your problem but I would use 我不知道这是否可以解决您的问题,但我会使用

 cta.hasClass("fixed"); 

to check if the class has already been applied to the element before you try adding it. 在尝试添加元素之前检查该类是否已应用于元素。

Necessary to use the Windows elements. 必须使用Windows元素。 Representative link below.... :) 下面的代表链接.... :)

Link 链接

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

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

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