Antonio
Antonio

Reputation: 395

Go to another page and trigger ScrollTo right after

The idea is click on a link to go to another page (index.html) and once you are there go to an anchor div immediately after. I have tried to do this with a trigger event but not working. The scrollTo is working in the index.html but I don't get to do that automatically from a backlink.

Here's my code:

$(".goTo").click(function() {
    /* If I'm in the index, it's working fine */
    if($('.show').parents('html').length > 0){
        $('body').animate({
            scrollTop: $(".works").offset().top
        }, 1500, 'easeInOutExpo');
    }else{
        location.href = "index.html";
        $(".goTo").trigger('click');
    }
});

Thanks in advance.

Upvotes: 0

Views: 139

Answers (1)

JTC
JTC

Reputation: 3464

You should use anchor in in your href location like this : location.href = "index.html#id";

and then get the id you want to scroll to like this : var hash = window.location.hash.substring(1);

So how your code would look:

$(".goTo").click(function() {
            window.location.href = "index.html#id";
        }
});

// Shorthand for document ready
$(function() {
 // This will get you everything behind the anchor !!
 var hash = window.location.hash.substring(1);
 var tag = $("#"+hash+"");
 // Animation
 $('html,body').animate({scrollTop: tag.offset().top},'slow');
});

Not tested, and not sure if this will work for you, but at least it shows you the course of action.

Upvotes: 1

Related Questions