Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery仅使用rel属性滚动_Javascript_Jquery_Html_Scroll_Anchor - Fatal编程技术网

Javascript jquery仅使用rel属性滚动

Javascript jquery仅使用rel属性滚动,javascript,jquery,html,scroll,anchor,Javascript,Jquery,Html,Scroll,Anchor,我有以下功能: 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,funct

我有以下功能:

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>
blabla
如果我点击链接:

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

只有在html链接中包含了特定的rel属性(例如“myscroll”)时,我才希望上述函数能够工作:


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

试试看


如果希望它与具有
rel
属性的所有元素一起工作

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

更新

试一试


如果希望它与具有
rel
属性的所有元素一起工作

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

更新

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