var html_tags = [];
\t var public_closest;
\t var from_top_pos;
\t var public_body_height;
\t
function start() {
\t \t when_scroll();
\t \t when_resize();
\t }
function when_resize() {
\t
\t \t var count_elements = document.body.childNodes;
\t \t var element = count_elements[public_closest];
\t \t var closest_pos = count_elements[public_closest].offsetTop;
\t \t var window_height = window.pageYOffset;
\t \t
\t \t window.scrollTo(0, closest_pos + from_top_pos);
\t }
function when_scroll() {
\t var window_height = document.body.scrollHeight;
\t
\t if (isNaN(public_body_height)) {
\t \t \t public_body_height = window_height;
\t \t }
\t
\t
\t if (public_body_height != window_height) {
\t \t public_body_height = window_height;
\t } else {
\t \t var how_many_elems = document.body.childNodes;
\t \t var closest_number;
\t \t var i;
\t \t
\t \t for (i = 0; i < how_many_elems.length; i++) {
\t \t \t \t html_tags[i] = how_many_elems[i].offsetTop;
\t \t \t }
\t \t \t \t
\t \t var closest;
\t \t
\t \t for (i = 0; i < html_tags.length; i++) { \t
\t \t \t if (isNaN(html_tags[i])) {
\t \t \t \t } else {
\t \t \t \t \t
\t \t \t \t \t if (isNaN(closest)) {
\t \t \t \t \t \t \t closest = html_tags[i];
\t \t \t \t \t \t }
\t \t \t \t \t \t \t
\t \t \t \t \t if (Math.abs(window.pageYOffset - html_tags[i]) <= Math.abs(window.pageYOffset - closest)) {
\t \t \t \t \t \t \t \t closest = html_tags[i];
\t \t \t \t \t \t \t \t closest_number = i;
\t \t \t \t \t \t \t } \t
\t \t \t \t }
\t \t \t }
\t \t \t
\t \t \t if (isNaN(public_closest)) {
\t \t \t \t } else {
\t \t \t \t \t how_many_elems[public_closest].style.border = "dotted 2px #33aaff";
\t \t \t \t }
\t \t \t
\t \t public_closest = closest_number;
\t \t how_many_elems[closest_number].style.border = "dotted 4px #FF55AA";
\t \t \t \t
\t \t var count_elements = document.body.childNodes;
\t \t var element = count_elements[public_closest];
\t \t var current_height = count_elements[public_closest].offsetTop;
\t \t var window_height = window.pageYOffset;
\t \t var height_difference = window_height - current_height;
\t \t
\t \t from_top_pos = height_difference;
\t }
}
body>div {
\t \t float:left;
\t \t width:35%;
\t \t min-width:200px;
\t \t min-height:200px;
\t \t max-height:1000px;
\t \t margin:30px 10px;
\t \t padding:20px 10px;
\t \t font-size:22px;
\t \t color:#33aaff;
\t \t background-color:#ffffff;
\t \t border:dotted 2px #33aaff;
\t \t overflow:hidden;
\t }
<body onload="start()" onscroll="when_scroll()" onresize="when_resize()">
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex. Donec vel urna vulputate, volutpat augue non, bibendum quam. Maecenas congue id justo nec feugiat. Mauris non molestie leo. Duis non tincidunt nibh. Duis tristique dapibus sapien id commodo. In et tempor lorem.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex. Donec vel urna vulputate, volutpat augue non, bibendum quam. Maecenas congue id justo nec feugiat. Mauris non molestie leo.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex. Donec vel urna vulputate, volutpat augue non, bibendum quam.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. ullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est.Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex. Donec vel urna vulputate, volutpat augue non, bibendum quam. Maecenas congue id justo nec feugiat. Mauris non molestie leo. Duis non tincidunt nibh. Duis tristique dapibus sapien id commodo. In et tempor lorem.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex. Donec vel urna vulputate, volutpat augue non, bibendum quam. Maecenas congue id justo nec feugiat. Mauris non molestie leo.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex. Donec vel urna vulputate, volutpat augue non, bibendum quam.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna. Mauris nec venenatis sapien, et auctor ex.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio. Nullam vel massa ut nisl sollicitudin ullamcorper a at urna.</p>
</div>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed urna pellentesque, commodo ante ut, porttitor est. Nullam viverra dignissim sem, ac auctor dui. Phasellus ut neque odio.</p>
</div>
</body>
我採取這種做法,在一個項目中我的工作,我在那裏拿到了第一個:可見對象和使用scrollTo嘗試主要滾動條的位置在瀏覽器中調整大小後。當用戶增加瀏覽器的大小時,大多數情況下它是有效的,但是當縮小時,事情就會變得很糟糕。我不得不添加代碼來檢測窗口是否縮小,並獲取LAST項目而不是第一個項目(如果窗口正在增長,這可以正常工作)。我用這個項目在桌面上敲了很多頭 - 希望這可以幫助別人嘻嘻 – Losbear 2016-11-30 16:30:26