jQuery:如何在页面加载时滚动到某个锚点/ div? [英] jQuery: how to scroll to certain anchor/div on page load?

查看:302
本文介绍了jQuery:如何在页面加载时滚动到某个锚点/ div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我试图更频繁地使用jquery而且现在我有一些问题我想用jquery来解决希望你能帮助我。

Lately im trying to use jquery more often and right now im having some problem i'd like to solve with jquery hope u could help me.

我有一些包含一些锚标记的网页(假设锚点位于页面中间)和onload上的事件我希望页面在某个锚标记位置上开始,这意味着页面将自动滚动到某个位置。

I have some web page that includes some anchor tag (lets say the anchor is located in the middle of the page) and on the event onload i want the page to start on that certain anchor tag location meaning the page will be "scrolled" automaticaly to a certain location.

这是我之前的解决方案(由于它将#i添加到我的网址,这非常难看)

That was my previous solution (which is quite ugly since it adds #i to my url)

window.onload = window.location.hash = 'i';

无论如何,你能告诉我怎样才能用jquery做到这一点?

Anyway could u tell me how can i do it with jquery?

注意:我不希望用户在到达此位置时感觉到任何幻灯片或效果

推荐答案

使用以下简单示例

function scrollToElement(ele) {
    $(window).scrollTop(ele.offset().top).scrollLeft(ele.offset().left);
}

其中 ele 是你的元素(jQuery)..例如: scrollToElement($('#myid'));

where ele is your element (jQuery) .. for example : scrollToElement($('#myid'));

这篇关于jQuery:如何在页面加载时滚动到某个锚点/ div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆