user2988257
user2988257

Reputation: 962

Pure onclick scrollTo

I am trying to invoke the scrollTo function directly from onclick action. Found many solutions but none of them not worked form me

I am trying to do this:

<button onclick="$(window).scrollTo('#mydiv',100);"type="button"  placeholder="" />CLICK ME</button>

<div id="mydiv">
</div>

Upvotes: 3

Views: 15103

Answers (2)

caramba
caramba

Reputation: 22480

try with animate() something like: http://jsfiddle.net/C8p6f/

<button class="myButton" type="button"  placeholder="" />CLICK ME</button>
<div class="oneDiv">yyy</div>
<div id="mydiv">xxx</div>

<script type="text/javascript">
    $(document).ready(function(){
        $('.myButton').on('click',function(){
            $('html, body').animate({scrollTop: $("#mydiv").offset().top}, 300);
        });
    });
</script>

Upvotes: 0

Drew
Drew

Reputation: 145

Try

<button onclick="$('html, body').animate({scrollTop: $('#mydiv').offset().top}, 2000);" type="button"  placeholder="" />CLICK ME</button>

<div id="mydiv"></div>

Make sure you use jQuery 1.9.x

Upvotes: 7

Related Questions