吃盐236 2021-05-21 22:40 采纳率: 100%
浏览 227
已采纳

js如何获取section的id名称内容?

刚开始学js,想做一个随着页面下滑改变位置和内容的时间线。

我打算让他的内容为离它最近的section的id名称,js代码如下:

var isDragging = false;
var sliderTop,pointerPos,currentSection;

var bodyHeight = $('body').height();
var sliderHeight = $('.slider-navigation').height();
var elementHeight = $('section').height();
var sectionAmount = $('section').length;
var scale = (bodyHeight - (elementHeight)) / (sliderHeight - $('.nav-pointer').outerHeight() );





function updatePos() {
  currentSection = $(window).scrollTop() / elementHeight;
  currentSectionName =  ;
  sliderTop = $(window).scrollTop() / scale;
  $('.nav-pointer').css('top', sliderTop).text(currentSectionNum);
}



$(window).scroll(function(){
  if (!isDragging) {
    updatePos();
  }
});

currentsectionName =部分不会写了 其它部分也不知道有没有错,请大佬解惑。

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-05-21 22:47
    关注

    这种效果?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <title>jquery实现章节目录效果</title>
    <script src="https://cdn.bootcss.com/jquery/1.7.1/jquery.min.js"></script>
    <style>
    body{background-attachment:fixed;background-image:url(about:blank)}/*修正IE6下浮动层闪动*/
    #anchor{position:fixed;top:150px;right:0px;border:solid 1px black;}
    #anchor.fixie6{position:absolute;top:expression(eval(document.documentElement.scrollTop)+150);}
    #anchor a{text-decoration:none;color:blue;}
    #anchor a.focus{background:red;color:white;}
    </style>
    <script>
        if (document.all) document.write('<!--[if lte IE 6]><script type="text/javascript">window.fixIE6= true<\/script><![endif]-->');//增加是否为IE6的判断
        window.onload = function () {
            var strict = document.compatMode == 'CSS1Compat', contents = $('div>a[name]:first-child'), anchors, s = [];
            contents.each(function () { s[s.length] = '<a href="#' + this.name + '">' + this.name + '</a>'; });
            anchors = $('<div id="anchor"' + (window.fixIE6 ? ' class="fixie6"' : '') + '>' + s.join('<br>') + '</div>').appendTo(document.body).find('a'); //添加文章的章节目录
            contents = contents.parent(); //更新章节锚点为内容节点
            //计算内容节点的上端点和下端点
            contents.each(function () { var o = $(this); o.data('top-bottom', { top: o.offset().top, bottom: o.offset().top + o.height() }); });
            $(window).scroll(function () {
                var viewTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop); //可见高度顶部
                var viewBottom = document[strict ? 'documentElement' : 'body'].clientHeight + viewTop; //可见高度底部
                contents.each(function (index, el) {
                    var tb = $(this).data('top-bottom');
                    if ((tb.top > viewTop && tb.top < viewBottom) || (tb.bottom > viewTop && tb.bottom < viewBottom) || (tb.top < viewTop && tb.bottom > viewBottom))
                        anchors.eq(index).addClass('focus');
                    else
                        anchors.eq(index).removeClass('focus');
                }).trigger('scroll');
            });
        };
    </script>
    <div style="height:800px"><a name="anchor1">anchor1</a></div>
    <div style="height:800px"><a name="anchor2">anchor2</a></div>
    <div style="height:800px"><a name="anchor3">anchor3</a></div>
    <div style="height:800px"><a name="anchor4">anchor4</a></div>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)