简体   繁体   中英

how can i scroll to #target - 100px?

I'm trying to use

<a href="#target">Details</a>

<div id="target">asd...</div>

but I also have top popup menu. So it's getting behind that menu.

Is it possible to write a JavaScript (also I'm already using bootstrap and jquery libraries on my template) go to #target - 100px

Thanks in advance

Never tried, but something like this should work.

 $('html, body').animate({
     scrollTop: $("#elementtoScrollToID").offset().top - 100
 }, 1000); // this is the time in millisecond of the animation

A simple JavaScript only solution is:

window.scrollTo(x,y)

For your example you would use:

window.scrollTo(0,100)

Step 1: Include Ariel Flesler's jquery.scrollTo-min.js

http://flesler.com/jquery/scrollTo/js/jquery.scrollTo-min.js

Step2: Add

$('a').scrollTo( $('#target'), 1000, {offset:-100} );
 scrollTop: ($($anchor.attr('href')).offset().top)-300

This is my code and it's works. -300 = pixels

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