使用javascript,如何让浏览器确保显示页面的一部分?
页面上有一个带滚动条的 DIV。试图确保特定项目对用户可见。
DIV 本身始终包含在页面的可见区域中,并且页面本身永远不会有滚动条。
例如,DIV 可能有 8 个段落,我想确保在某些情况下用户现在可以看到 DIV 中的第 5 段。
have a DIV with a scroll bar on the page. trying to ensure a particular item is visible to the user.
the DIV itself is always contained in the visible area of the page, and the page never has a scrollbar itself.
for example, the DIV might have 8 paragraphs, and I want to make sure in certain situations that the user can see paragraph 5 within the DIV right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在您想要可见的段落中使用锚标记,每当出现您想要确保该部分可见的情况时,您可以通过 javascript 链接到锚标记。
示例如下
通过所需的段落创建一个锚点
移动到锚点的 javascript
剩下的就是在需要时调用此脚本。您可以通过 body 标签中的加载或通过您认为合适的其他方式来完成
You could use an anchor tag by the paragraph you want to be visible, whenever the situation arises where you want to be sure that this section is visible you can link to the anchor through javascript.
Example follows
Create an anchor by the desired paragraph
The javascript to move to your anchor
What remains is to call this script when you desire. you could do it with an onload in the body tag or through some other means you feel appropriate