简体   繁体   English

滚动到div中具有固定高度的元素

[英]scroll to elements inside a div with fixed height

I have a <div> with a fixed height and overflow-y: scroll . 我有一个固定高度和<div> overflow-y: scroll<div> overflow-y: scroll Inside this div, I have primarily a <p> tag containg a long text with some highlithing (spans with background-color and a numbered id attribute). 在此div内,我主要有一个<p>标记,其中包含带有一些高修饰度的长文本(具有background-color和numbered id属性的跨度)。

By the way, it is a HTMl5 application with AngularJS. 顺便说一下,它是带有AngularJS的HTMl5应用程序。

Now I like to implement a button to jump through the highlighted positions: I like to get the div scrolled to the right position and the rest of the page shall stay untouched ie "unscrolled". 现在,我想实现一个按钮来跳转到突出显示的位置:我想使div滚动到正确的位置,并且页面的其余部分将保持不变,即“未滚动”。 How can I achieve that the div is scrolled to the right position and not the whole page is scrolled down disturbing the page layout? 如何实现div滚动到正确的位置,而不是整个页面都向下滚动而干扰页面布局?

On principle, I know that I can use hashtag + id in the url to go to the element with a given id - I also found $anchorScroll from AngularJS but it seems not to be the right way, since it scrolles down the whole browser content instead of just inside my scrollable div. 原则上,我知道我可以在url中使用hashtag + id转到具有给定id的元素-我还从AngularJS中找到$ anchorScroll,但这似乎不是正确的方法,因为它向下滚动了整个浏览器内容而不是仅放在我的可滚动div中。

Years ago, as using an iframe was not ugly, it was easy to use an iframe for this; 几年前,由于使用iframe并不难看,因此使用iframe很容易。 however, today, I think there must be better solutions. 但是,今天,我认为必须有更好的解决方案。

Any help is appreciated! 任何帮助表示赞赏! Thanks in advance. 提前致谢。

This question, if I understand it correctly, has been already answered here if you are willing to use JS and JQuery. 如果您正确使用此问题,如果您愿意使用JS和JQuery,则已经在此处回答。 Just attach the code suggestion to a button. 只需将代码建议附加到按钮即可。

https://stackoverflow.com/a/5267018/5797159 https://stackoverflow.com/a/5267018/5797159

Here's an example that might help you. 这是一个可能对您有所帮助的示例。 It uses jQuery to set the scrollTop on a target <div> without scrolling the rest of the document. 它使用jQuery在目标<div>上设置scrollTop而不滚动文档的其余部分。

To use it, enter a number between 0-99 in the <input> field and click Submit . 要使用它,请在<input>字段中输入一个介于0-99之间的数字,然后单击Submit The div should scroll to the top of the <span> element (within the target div ) with that numeric id . div应该滚动到具有该数字id<span>元素的顶部(在目标div )。

Note that the value in the call to scrollTop() is corrected for the height of the target element. 请注意,对scrollTop()的调用中的值已针对目标元素的高度进行了校正。

 for (var i = 0; i < 100; i++) { // http://www.paulirish.com/2009/random-hex-color-code-snippets/ var randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16); $("#myScrollingDiv").append('<p>#' + i + ': text <span style="background-color:' + randomColor + '" id="' + i + '">text</span> text</p>') } $("#myForm").submit(function(event) { event.preventDefault(); var idNumber = $("#idNumber").val() var targetElem = $("#" + idNumber); var targetOffset = targetElem.offset(); var divScrollTop = $('#myScrollingDiv').scrollTop(); $('#myScrollingDiv').scrollTop(divScrollTop + targetOffset.top - targetElem.height()); }); 
 body { height: 5000px; overflow-y: scroll; } #myScrollingDiv { overflow-y: scroll; height: 100px; width: 150px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myScrollingDiv"></div> <form id="myForm"> <input id="idNumber" type="number"> <input type="submit"> </form> Enter a numeric ID: 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM