Question

i have following function:

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();
    }
});

that permit to scroll to the specific div element

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

if i click on link :

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

i want that function above work only if in html link i have included a specific rel attribute for example "myscroll":

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

how to modify function for work in this mode? thanks

Was it helpful?

Solution

Try Attribute Equals Selector [name="value"]

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


Has Attribute Selector [name]

If you want it to work with all elements with rel attribute

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


Update

$('a[href^=#],a[rel="myscroll"]').bind("click", jump);
           //^ works for both a start with # and with rel="myscroll"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top