如果没有其他的数据属性添加到元素?

时间:2016-11-03 11:50:15

标签: jquery

我问,因为没有找到任何正常的工作示例..当你滚动页面时应该工作data-active =" on"当反向数据激活="关闭"

var winHeight = $(window).height();
    var thisY = $(this);
    var thisY = e.offset().top;

    if (thisY <= (windowHeight * 0.85)) {
       var dataActiveOn = $(this).attr('data-active', 'on');
    }
    else {
        var dataActiveOff = $(this).attr('data-active', 'off');
    }

html代码:

<section class="js-monitor-scroll" data-active="off"></section>

2 个答案:

答案 0 :(得分:0)

请参阅此片段。

&#13;
&#13;
function changeAttr()
{
  console.log($(".js-monitor-scroll").attr("data-active"));
  $(".js-monitor-scroll").attr("data-active", "on");
  console.log($(".js-monitor-scroll").attr("data-active"));
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="js-monitor-scroll" data-active="off"></section>
<button onclick="changeAttr()">Change attribute</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用它而不是你所做的。更简单:

thisY = $(window).scrollTop();

并删除var in if else ...直接执行.attr方法......