【问题标题】:jquery scroll only with rel attributejquery 仅使用 rel 属性滚动
【发布时间】:2014-02-19 17:08:38
【问题描述】:

我有以下功能:

var jump=function(e)
{
   if (e){
       e.preventDefault();
       var target = $(this).attr("href");
   }else{
       var target = location.hash;
   }

   $('html,body').animate(
   {
       scrollTop: $(target).offset().top
   },2000,function()
   {
       location.hash = target;
   });

}

$('html, body').hide();

$(document).ready(function()
{
    $('a[href^=#]').bind("click", jump);

    if (location.hash){
        setTimeout(function(){
            $('html, body').scrollTop(0).show();
            jump();
        }, 0);
    }else{
        $('html, body').show();
    }
});

允许滚动到特定的 div 元素

<div id="anchor_element">blabla</div>

如果我点击链接:

<a href="#anchor_element">Scroll here</a>

我希望上面的功能只有在 html 链接中包含特定的 rel 属性(例如“myscroll”)时才起作用:

<a href="#anchor_element" rel="myscroll">Scroll here</a>

如何修改此模式下的工作功能? 谢谢

【问题讨论】:

    标签: javascript jquery html scroll anchor


    【解决方案1】:

    试试Attribute Equals Selector [name="value"]

    $('a[href^=#][rel="myscroll"]').bind("click", jump);
    


    Has Attribute Selector [name]

    如果您希望它与具有rel 属性的所有元素一起使用

    $('a[href^=#][rel]').bind("click", jump);
    


    更新
    $('a[href^=#],a[rel="myscroll"]').bind("click", jump);
               //^ works for both a start with # and with rel="myscroll"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多