简体   繁体   English

滚动到div中的元素时更新锚标签

[英]Update anchor tag as you scroll past an element in a div

I am not sure if this is possible that is why I am on here asking so if you have any ideas on how this would be possible then I would greatly appreciate it! 我不确定这是否可行,这就是为什么我要在这里问,所以如果您对如何实现有任何想法,那么我将不胜感激!

Ok say that I have a div that scrolls with overflow-y feature. 好的,我有一个带有溢出Y功能滚动的div。 And say that inside of this div there is a story or large amount of text. 并说在这个div内部有一个故事或大量文字。 Is there anyway to have Javascript memorize or save where you are at in this div maybe through a line number or anchor tag possibly? 无论如何,是否有可能通过行号或锚标记来记住或保存Java在这个div中的位置? That way whenever the div is gone, such as if I had javascript change the content of the div, I can prompt the user to click a button and it will return to the spot they were originally at? 这样,只要div消失了(例如,如果我用JavaScript更改了div的内容),我都可以提示用户单击一个按钮,它将返回到原来的位置?

I have no approach at this at all! 我根本没有办法! :( The farthest I can think is the simple way of giving each line an anchor tag which is very repetitive and not worth the time and I still wouldn't know how to get the anchor tag on scroll over. :(我能想到的最远的是给每行一个锚标记的简单方法,该锚标记是重复性的,不值得花时间,我仍然不知道如何在滚动时获得锚标记。

<div style="overflow-y:scroll;height:200px;"> 

<span id="1">This would</span>

<span id="2">be too</span>

<span id="3"> repetitive and</span>

<span id="4">still dont know</span>

<span id="4">how to save the anchor</span>

 </div>

I was thinking for the javascript maybe something like this. 我在想JavaScript,也许是这样的。

<a href="#" onclick="document.getElementById('***CURRENTSPOT***').id = 'spot';">Save Spot</a>

Then to reload all they would have to do is click this 然后要重新加载所有他们需要做的就是单击此

<a href="#spot">Load Saved Spot</a>

You can try saving the scrollTop property of the parent DOM element and setting that again when the user loads the saved spot. 您可以尝试保存父DOM元素的scrollTop属性,并在用户加载保存的位置时再次进行设置。

Check out this MDN article for more details 查看此MDN文章以了解更多详细信息

Heres a quick and dirty implementation: http://jsbin.com/ezexaf/1/edit 这是一个快速而肮脏的实现: http : //jsbin.com/ezexaf/1/edit

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

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