0

I have this code:

<script type='text/javascript'>//<![CDATA[ 
    $(window).load(function() {
        $(".scroll").click(function(event) {
            event.preventDefault();
            //calculate destination place
            var dest = 0;
            if ($(this.hash).offset().top > $(document).height() - $(window).height()) {
                 dest = $(document).height() - $(window).height();
            } else {
                 dest = $(this.hash).offset().top;
            }
            //go to destination
            $('html,body').animate({ scrollTop: dest }, 1000, 'swing');
        });
    });//]]>
</script>

How should I do to get the scrolling to such #destination immediately when the page loads instead of as now when I have to click on a link first?

Regent
  • 4,986
  • 3
  • 19
  • 34
Andreas
  • 113
  • 2
  • 4
  • 10
  • You can add this to the document read to trigger the click event when the page is loaded .. $( ".scroll" ).trigger( "click" ); – Bob Tate Apr 21 '15 at 12:08
  • If you don't need animate affect for scroll on page load, you can just pass your element id in url with # prefixed. Like `www.site.com/page#scroll-here`. It will scroll to the element with id `scroll-here`. – Airan Apr 21 '15 at 12:12
  • BwithLove: Thanks but i need the animate affect :/ – Andreas Apr 21 '15 at 12:14
  • In that case, once page loads, you can trigger `click` method for your element as `Bob Tate` said. – Airan Apr 21 '15 at 12:21
  • I try this: $(window).load(function(){ $(".scroll").trigger("click"); $(".scroll").click(function(event){ But not working – Andreas Apr 21 '15 at 12:55
  • `$(".scroll").trigger("click"); ` call it after `$(".scroll").click(function(event){...});` – Airan Apr 21 '15 at 13:02

0 Answers0