Trying to get div to view

I tried to get the div in the view, and div elements are added dynamically .. so I assign a link from which to click the div element can be introduced into the view, regardless of what it means. so when I click on the link, if the div im trying to jump to the view is already in view, it will disappear from my view. this is mistake?

http://jsfiddle.net/xFu3M/6/

Ive added a working example.

the code looks like

$(".testClick").on("click",function(e){ e.preventDefault(); // Call the scroll function goToByScroll("indID"+1); }); function goToByScroll(id){ // Reove "link" from the ID id = id.replace("link", ""); // Scroll $('.contentBody').animate({ scrollTop: $("#"+id).offset().top}, 'slow'); } 
+4
source share
1 answer

It would be useful to take into account the parental bias:

Live demo

 $(document).ready(function(){ $(".wrapper").css({"width" : $(window).width() , "height" : $(window).height()} ); $(".testClick").on("click",function(e){ e.preventDefault(); goToByScroll("indID1"); // try change to indID4 }); }); function goToByScroll(id){ var el = $('#'+id); var elOffs = el.offset().top; var parOffs = el.closest('.contentBody').offset().top; $('.contentBody').stop().animate({ scrollTop: elOffs - parOffs },800); } 
+1
source

All Articles