简体   繁体   中英

Scrolling with anchor in jquery lightbox

I have a requirement to scroll to a paragraph on click of a link inside a light box. I tried using the below code

function goToByScroll(id) {
    $('html,body').animate({scrollTop: $("#"+id).offset().top}, 'slow');
}

This one scrolls the complete page at the background and not to the contents in the lightbox at front. I tried using the id of lightbox to animate instead of ('html,body') but no luck. Is there a way to do it?

You can use something like this by using the id given to your paragraph:

 $("#button").click(function() { $('html, body').animate({ scrollTop: $("#my_paragraph").offset().top }, 2000); }); 

I've made you a fiddle here

You have to give your anchor the id 'button' and your paragraph the id 'my_paragraph' or whatever suits your needs.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM