Scroll to the center of viewport
问题 I would like to center a div by clicking it. So if I'm clicking a div I want it to scroll to the center of the browser viewport. I don't want to use anchor points like the guides and examples I've seen. How can I achieve this? 回答1: In some way you have to identify the clickable elements. I build an example, that uses the class -attribute for that. Step 1 This is the script, that does the work: $('html,body').animate({ scrollTop: $(this).offset().top - ( $(window).height() - $(this)