简体   繁体   中英

scroll to animate selected anchor in jquery

I have simple JS function that's animate page scroll to selected anchor by id

all anchor in my page have different offset value and position tip: I user masonry effect but does not work correctly

function scrollToAnchor(aid) {
    var aTag = $("a[id='" + aid + "']");
    $('html,body').animate({ scrollTop: aTag.position().top  }, 'slow');
}

You should use offset instead of position().

But I know by experience that Masonry by default doesn't use position absolute & top, left but it uses a css3-transform instead. And this means that .left and .top will output 0 instead of the css3-transform-value.

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