27

I need Jquery's scrollTo function to work for this website: http://cinicraft.com/Silverman/index.html

I've tried the following

$(document).ready(function()
{
    $("div.btnLp3").click(function(){
        $('body,html').scrollTo('#target-examples', 800, {easing:'elasout'});
    });
});

And I've tried this too:

$(document).ready(function()
{
    $("div.btnLp3").click(function(){
        $('html, body').animate({ scrollTop: $(window.location.hash).offset().top}, 1000);
    });
});

I can't seem to get scrollTo working at all. Does anyone have any ideas? Here's what I have imported:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">  </script>    
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js">  </script>
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
Matt Andrzejczuk
  • 2,001
  • 9
  • 36
  • 51

3 Answers3

52

Try this

$("#clickme").click(function() {
    $('html, body').animate({
        scrollTop: $("#wrap2").offset().top
    }, 2000);
    return false;
});

FIDDLE

Khawer Zeshan
  • 9,470
  • 6
  • 40
  • 63
  • 3
    I always like to add a bit of offset, gives the viewer some breathing space around the content. Like so `.offset().top - 20}` – user2898276 May 09 '14 at 15:52
  • 4
    this does not answer the question of why the user is getting the error 'scrollTo is not a function ' most will arrive at this question after searching for 'scrollTo is not a function ' You don't even use the 'scrollTo' function in question - in your answer. How does using completely different functions answer the question of why the op is getting errors on 'scrollTo'? –  Jun 24 '20 at 17:33
4
/*
* ScrollToElement 1.0
* Copyright (c) 2009 Lauri Huovila, Neovica Oy
*  lauri.huovila@neovica.fi
*  http://www.neovica.fi
*  
* Dual licensed under the MIT and GPL licenses.
*/

(function($) {
    $.scrollToElement = function($element, speed) {

        speed = speed || 750;

        $("html, body").animate({
            scrollTop: $element.offset().top,
            scrollLeft: $element.offset().left
        }, speed);
        return $element;
    };

    $.fn.scrollTo = function(speed) {
        speed = speed || "normal";
        return $.scrollToElement(this, speed);
    };
})(jQuery);
CountZero
  • 6,171
  • 3
  • 46
  • 59
1
 $("#your-div")[0].scrollTo(0, Number.MAX_SAFE_INTEGER);
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
grecio beline
  • 51
  • 1
  • 4
  • 4
    Although this code might solve the problem, a good answer should also explain what it does and how it helps. – Suraj Kumar Mar 26 '20 at 12:40
  • Sorry, So, First I find the target element that will be affected by the scroll (in this case I used jquery to find the element by Id). So I use The scrollTo () https://www.w3schools.com/jsref/met_win_scrollto.asp – grecio beline Apr 01 '20 at 13:47