MShack MShack - 7 months ago 17
Javascript Question

Scroll to top on click of onclick cell

I have 2 tables set up, and on mobile I hide one and show the other

onclick
of a
tablerow
or icon, I need to have offset position set to top when I click, but having issues getting this done

When I click on this HTML

<a title="" style="cursor:pointer" onclick="updateStarter('1137','')"></a>


I want the shown table to open up at top of
div#right-head


When I click on this HTML

<tr class="eventablerow" style="cursor:pointer" onclick="showLineupTab(1,true)"></tr>


I want the shown table to open up at top of
div#left-head


I have this working on one, but I wanted to be more specific to the
a
and
tr
for both instances when I click as I have other data in the tables you can click on, and when you do it causes
scrollTo
top to trigger

var jump = $(this).attr('href');
var new_position = $('#left-head').offset();
window.scrollTo(new_position.left,new_position.top);

Answer

Well I'm not sure of the real position of your table and div#right-head but I can suggest you to try this:

$(' a , tr').click(function(){
    var jump = $(this).attr("href");
    var _top = $("#left-head").offset().top;
    $("html, body").animate({"scrollTop":_top+"px"}, 1000);
    return true;
});