简体   繁体   English

在标题中显示ID属性

[英]Display id attribute in header

I have this page: 我有这个页面:

http://i.stack.imgur.com/AfPWJ.jpg http://i.stack.imgur.com/AfPWJ.jpg

http://jsfiddle.net/fLGQJ/ http://jsfiddle.net/fLGQJ/

I want to capture on which div I am while I'm scrolling. 我想捕捉滚动时所在的div。 in header by displaying id attribute in header 在标题中显示标题中的id属性

Something like: 就像是:

var a =   // the topmost div
( $(document).scrollTop() > $(a).position().top) {    
    console.log($(a).attr('id'))
}

I am looking to display id attribute of the div in header 我想在标题中显示div的id属性

Can I achieve that without a plugin, simply 2-3 lines? 如果没有插件,只需2-3行,我能否实现? or even a plugin if any 甚至是插件(如果有)

this is the html example 这是HTML示例

  <div class="vs" id="Genesis 1:1">
    &nbsp;1 In the <a href="GenesisRef.html#Genesis%201">a</a>beginning <a href="GenesisRef.html#Genesis%202">b</a>God created the heaven and the earth.
  </div>

  <div class="vs" id="Genesis 1:2">
    &nbsp;2 And the earth was without form, and void; and darkness was upon the face of the deep. <a href="GenesisRef.html#Genesis%203">c</a>And the Spirit of God moved upon the face of the waters.
  </div>

  <div class="vs" id="Genesis 1:3">
    &nbsp;3 <a href="GenesisRef.html#Genesis%204">d</a>And God said, <a href="GenesisRef.html#Genesis%205">e</a>Let there be light: and there was light.
  </div>

  <div class="vs" id="Genesis 1:4">
    &nbsp;4 And God saw the light, that it was good: and God divided the light from the darkness.
  </div>

  <div class="vs" id="Genesis 1:5">
    &nbsp;5 And God called the light <a href="GenesisRef.html#Genesis%206">f</a>Day, and the darkness he called Night. And the evening and the morning were the first day.
  </div>

  <div class="vs" id="Genesis 1:6">

    &nbsp;6 And God said, <a href="GenesisRef.html#Genesis%207">g</a>Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.
  </div>

  <div class="vs" id="Genesis 1:7">
    &nbsp;7 And God made the firmament, <a href="GenesisRef.html#Genesis%208">h</a>and divided the waters which were under the firmament from the waters which were <a href="GenesisRef.html#Genesis%209">i</a>above the firmament: and it was so.
  </div>

  <div class="vs" id="Genesis 1:8">

    &nbsp;8 And God called the firmament Heaven. And the evening and the morning were the second day.
  </div>

  <div class="vs" id="Genesis 1:9">
    &nbsp;9 And God said, <a href="GenesisRef.html#Genesis%2010">j</a>Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so.
  </div>

  <div class="vs" id="Genesis 1:10">
    &nbsp;10 And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good.
  </div>

for example if div 5 is at the top then Genesis 1:5 should be displayed 例如,如果div 5位于顶部,则应显示创世记1:5

I think you might be looking for 我想您可能正在寻找

a.appendTo("#Header-Div");

which would go after your other coding. 这将遵循您的其他编码。 and just replace header-div for whatever the id is of the box you want it to go into. 并只要将header-div替换为您想要放入的框的ID即可。

How about this: 这个怎么样:

if( $(document).scrollTop() > $(a).position().top) {    
$("#header").html(a.attr('id'));
}
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
    $(window).scroll(function() {

        var winTop = $(this).scrollTop();
        var divs = $('div');

        var top = $.grep(divs, function(item) {
            return $(item).position().top <= winTop;
        });


        //var id=$(top).first().attr('id');
        var ll=$(top);



    $(top).each(function(i, item) {
    var total=ll.length-1;

    if(total==i)
    {
    $("#finalvalue1").empty();
 document.title=$(item).attr("id");
}
});



    });

this code worked for me thanks everyone for help :) 此代码为我工作,谢谢大家的帮助:)

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

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