jquery scrollTop 在 IE8 中不起作用
我正在尝试根据网址上的哈希值滚动页面。这是我的代码:
var hash = window.location.hash;
$(hash).scrollTop();
这没有任何作用。那么我做错了什么? 另一件事,我需要这样的东西:
$(hash).scrollTop($("#header").height());
这可能吗?..我的 div(元素哈希指向)是否要滚动到顶部,直到 "#header"
的底部?..
更新 1
哈希文本是一个 id,因此它从 window.location.hash
返回文本“#myid”。另一件事是,标头的 div 在其 css 上有一个 position:fixed
,而 div(哈希值所在的容器)有一个 position:absolute
> 所以它在 #header
下滚动,这就是为什么我需要它滚动到标题底部或高度。
更新 2
我现在尝试使用 scrollIntoView() 插件 但出现错误。
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
Timestamp: Mon, 17 Oct 2011 03:23:18 UTC
Message: Object doesn't support this property or method
Line: 71
Char: 5
Code: 0
URI: http://localhost:3000/javascripts/jquery.scrollIntoView.js
Message: Object doesn't support this property or method
Line: 34
Char: 5
Code: 0
URI: http://localhost:3000/surveys/%E6%83%85%E5%A0%B1%E3%82%BB%E3%82%AD%E3%83%A5%E3%83%AA%E3%83%86%E3%82%A3%E3%83%BB%E5%80%8B%E4%BA%BA%E6%83%85%E5%A0%B1%E4%BF%9D%E8%AD%B7%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF%E3%82%B7%E3%83%BC%E3%83%88%EF%BC%88%E8%87%AA%E5%B7%B1%E7%82%B9%E6%A4%9C%EF%BC%89-%E7%89%88-1-0/C9uKCqXNn2/take?section=17
我的代码是: $("#survey_section_8").scrollIntoView();
我在这里做错了什么?...
希望我的问题很清楚。谢谢你!
I'm trying to scroll the page based on the hash on the url. Here's my code:
var hash = window.location.hash;
$(hash).scrollTop();
This doesn't to anything. So what am I doing wrong?
And another thing, I need something like this:
$(hash).scrollTop($("#header").height());
Is that possible?..is my div(the element hash is pointed to) going to scroll to the top, upto the bottom of the "#header"
?..
Update 1
The hash text is an id so it returns the text "#myid" from window.location.hash
. One other thing, the div of the header has a position:fixed
on its css while the container where the div(the hash is id'd to) has a position:absolute
so it is scrolling under the #header
that's why I need it to scroll to the bottom of the header or the height.
Update 2
I'm now trying to use the scrollIntoView() plugin but having the error.
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
Timestamp: Mon, 17 Oct 2011 03:23:18 UTC
Message: Object doesn't support this property or method
Line: 71
Char: 5
Code: 0
URI: http://localhost:3000/javascripts/jquery.scrollIntoView.js
Message: Object doesn't support this property or method
Line: 34
Char: 5
Code: 0
URI: http://localhost:3000/surveys/%E6%83%85%E5%A0%B1%E3%82%BB%E3%82%AD%E3%83%A5%E3%83%AA%E3%83%86%E3%82%A3%E3%83%BB%E5%80%8B%E4%BA%BA%E6%83%85%E5%A0%B1%E4%BF%9D%E8%AD%B7%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF%E3%82%B7%E3%83%BC%E3%83%88%EF%BC%88%E8%87%AA%E5%B7%B1%E7%82%B9%E6%A4%9C%EF%BC%89-%E7%89%88-1-0/C9uKCqXNn2/take?section=17
My code is: $("#survey_section_8").scrollIntoView();
what am I doing wrong here?..
Hope that I was clear in my questions. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要类似这样的内容才能滚动到指定的锚点:
请注意,您需要在
中添加一些文本(例如,只是一个空格)才能获得
偏移量
。You need something more like this to scroll to the specified anchor:
Note that you'll need some text within that
<a>
(e.g. just a space) to be able to get theoffset
.$(window.location.hash)
不会做太多事情,因为那只是一个字符串。 jQuery 需要更多指令来查找您要查找的元素。哈希文本是页面上元素的 ID、类或名称吗?您可能需要在其前面加上“.”前缀。或“#”或与其他选择器组合,以便 jQuery 找到生成哈希的锚元素。
其次,调用
.scrollTop()
而不带任何参数返回< /em> 滚动顶部位置;它不会滚动到顶部。向它传递一个参数可以设置调用它的元素的垂直滚动位置。 @N Rohler 的答案比我凭空想出的更接近一个工作示例。这对你来说应该是一个好的开始。$(window.location.hash)
isn't going to do you much, because that's just a string. jQuery needs more instruction to find the element you're looking for.Is the hash text the id or class or name of an element on the page? You're probably going to need to prefix it with a "." or a "#" or in combination with some other selector in order for jQuery to find the anchor element that generated the hash.
Secondly, calling
.scrollTop()
without any parameters returns the scroll-top position; it doesn't scroll to the top. Passing a parameter to it sets the vertical scroll position for the element it was called on. @N Rohler's answer is much closer to a working example than I could have come up with off the top of my head. It should be a good start for you.我最终使用了 这个插件 jQuery.ScrollTo()。但我仍然无法摆脱这个错误。所以我尝试将我的代码放入插件的 javascript 中。尤里卡!成功了!我认为该错误是由页面内的其他 jquery 引起的。这是我在 javascript 中插入的内容:
我知道这不是最好的答案。但它完成了工作,所以没关系!稍后我会弄清楚如何优化它。
I ended up using this plugin jQuery.ScrollTo(). But still I can't get rid off that error. So I tried putting my code inside the javascript of the plugin. Eureka! it worked! I think the error is caused by the other jquery inside the page. This is what I have inserted in the javascript:
I know this isn't the best answer. But it did the job, so it's alright! I'll figure it out later on how to optimize it.