HTML 和 jQuery 锚定
每当 url 包含 div id 时,当 URL 具有以下内容时,它显然会转到 div:
<div id="1">Bla bla bla</div>
但我喜欢的是与 Stackoverflow 相同的功能是,当您单击消息中的答案时,它将向下滚动到页面并对答案具有淡出效果。
我该怎么做?
Whenever the url contains the div id, it would obviously go down to the div when the URL has:
<div id="1">Bla bla bla</div>
But what I like is to have same feature of Stackoverflow, when you click on an answer in your messages, it will scroll down to the page and has that fadeOut effect on the answer.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,到有效锚点目标的动画不能在页面加载时进行动画处理,因为浏览器默认将用户向下滚动页面到锚点。对于页内链接,您可以劫持锚链接并设置动画。
但是,在像 SO 这样的新页面加载时,您会注意到页面不会向下动画,而只是向下滚动,尽管该框确实会动画颜色。这就是您在 jQuery 中可以做到的方法。如果您想为背景设置动画,请务必包含
color
插件-颜色。您可以使用 DOMReady 代替 load,但它可能会过早尝试运行动画,而用户会错过它。
如果您只想使用特定类对
div
进行动画处理,则可以在查找中添加过滤器:Animation to a valid anchor destination cannot be animated on page load that I know of since the browsers will default to scrolling the user down the page to the anchor. For in-page links, you can hijack the anchor links and animate.
However, on new page loads like on SO, you will notice the page does not animate down, but just scrolls down, though the box does animate a color. This is how you could do it in jQuery. Be sure to include the
color
plugin if you want to animate background-colors.You can use DOMReady instead of load, but it might try to run your animation too soon, and the user will miss it.
If you only wanted to animate
div
's with a specific class, you can add a filter to your find:用途:
例如:
Use:
For example:
StackOverflow 也使用锚点。您当前正在阅读的帖子是:
HTML 和 jQuery 锚定
这只是
现在,要获得淡入淡出效果[在没有框架的纯 JS 中]
设置 div.style.opacity = 0;
clearInterval 部分不是必需的,因为一旦不透明度超过 1,浏览器将不会以不同的方式呈现[尽管数字不断增加...]
StackOverflow uses anchors as well. The post you're currently reading is:
HTML and jQuery anchoring
It's simply
<a name="anchorName"></a>
at the address bar: [urlToPage]#anchorName
Now, to get the fade effect [in pure JS w/o frameworks]
Set the div.style.opacity = 0;
The clearInterval part isn't necessary, since once opacity goes above 1, browser won't render differently [although the number keeps adding...]