求百度百科右侧底部js导航代码,左侧导航栏代码

本文目录一览:

类似百度百科右侧的js效果怎么做

使用scroll事件监听,计算滚动位置,下面是思路,不一定运行正确,有什么问题可以再交流,

假设页面上有有几个内容块:

div id=”content1″

  h3家庭情况/h3

  div…/div

/div

div id=”content2″

  h3人物评价/h3

  div…/div

/div

div id=”content3″

  h3作品/h3

  div…/div

/div

控制逻辑:

var content1 = document.getElementById(‘content1’);

var content2 = document.getElementById(‘content2’);

var content3 = document.getElementById(‘content3’);

window.addEventListener(‘scroll’, function() {

  // 拿到滚动位置

  var scrollY = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;

  // 计算悬浮层高亮

  if (content3.getBoundingClientRect().top  scrollY) {

    // 改变右侧悬浮层高亮content3标题

  } else if (content2.getBoundingClientRect().top  scrollY) {

    // 改变右侧悬浮层高亮content2标题

  } else if (content1.getBoundingClientRect().top  scrollY) {

    // 改变右侧悬浮层高亮content1标题

  }

}, false);

百百度百科右侧导航滚动代码

function DirectoryNav($h,config){

this.opts = $.extend(true,{

scrollThreshold:0.5,    //滚动检测阀值 0.5在浏览器窗口中间部位

scrollSpeed:700,        //滚动到指定位置的动画时间

scrollTopBorder:500,    //滚动条距离顶部多少的时候显示导航,如果为0,则一直显示

easing: ‘swing’,        //不解释

delayDetection:200,     //延时检测,避免滚动的时候检测过于频繁

scrollChange:function(){}

},config);

this.$win = $(window);

this.$h = $h;

this.$pageNavList = “”;

this.$pageNavListLis =””;

this.$curTag = “”;

this.$pageNavListLiH = “”;

this.offArr = [];

this.curIndex = 0;

this.scrollIng = false;

this.init();

}

DirectoryNav.prototype = {

init:function(){

this.make();

this.setArr();

this.bindEvent();

},

make:function(){

//生成导航目录结构,这是根据需求自己生成的。如果你直接在页面中输出一个结构那也挺好不用 搞js

$(“body”).append(‘div class=”directory-nav” id=”directoryNav”ul/ulspan class=”cur-tag”/spanspan class=”c-top”/spanspan class=”c-bottom”/spanspan class=”line”/span/div’);

var $hs = this.$h,

$directoryNav = $(“#directoryNav”),

temp = [],

index1 = 0,

index2 = 0;

$hs.each(function(index){

var $this = $(this),

text = $this.text();

if(this.tagName.toLowerCase()==’h2′){

index1++;

if(index1%2==0) index2 = 0;

temp.push(‘li class=”l1″span class=”c-dot”/span’+index1+’. a class=”l1-text”‘+text+’/a/li’);

}else{

index2++;

temp.push(‘li class=”l2″‘+index1+’.’+index2+’ a class=”l2-text”‘+text+’/a/li’);

}

});

$directoryNav.find(“ul”).html(temp.join(“”));

//设置变量

this.$pageNavList = $directoryNav;

this.$pageNavListLis = this.$pageNavList.find(“li”);

this.$curTag = this.$pageNavList.find(“.cur-tag”);

this.$pageNavListLiH = this.$pageNavListLis.eq(0).height();

if(!this.opts.scrollTopBorder){

this.$pageNavList.show();

}

},

setArr:function(){

var This = this;

this.$h.each(function(){

var $this = $(this),

offT = Math.round($this.offset().top);

This.offArr.push(offT);

});

},

posTag:function(top){

this.$curTag.css({top:top+’px’});

},

ifPos:function(st){

var offArr = this.offArr;

//console.log(st);

var windowHeight = Math.round(this.$win.height() * this.opts.scrollThreshold);

for(var i=0;ioffArr.length;i++){

if((offArr[i] – windowHeight) st) {

var $curLi = this.$pageNavListLis.eq(i),

tagTop = $curLi.position().top;

$curLi.addClass(“cur”).siblings(“li”).removeClass(“cur”);

this.curIndex = i;

this.posTag(tagTop+this.$pageNavListLiH*0.5);

//this.curIndex = this.$pageNavListLis.filter(“.cur”).index();

this.opts.scrollChange.call(this);

}

}

},

bindEvent:function(){

var This = this,

show = false,

timer = 0;

this.$win.on(“scroll”,function(){

var $this = $(this);

clearTimeout(timer);

timer = setTimeout(function(){

This.scrollIng = true;

if($this.scrollTop()This.opts.scrollTopBorder){

if(!This.$pageNavListLiH) This.$pageNavListLiH = This.$pageNavListLis.eq(0).height();

if(!show){

This.$pageNavList.fadeIn();

show = true;

}

This.ifPos( $(this).scrollTop() );

}else{

if(show){

This.$pageNavList.fadeOut();

show = false;

}

}

},This.opts.delayDetection);

});

this.$pageNavList.on(“click”,”li”,function(){

var $this = $(this),

index = $this.index();

This.scrollTo(This.offArr[index]);

})

},

scrollTo: function(offset,callback) {

var This = this;

$(‘html,body’).animate({

scrollTop: offset

}, this.opts.scrollSpeed, this.opts.easing, function(){

This.scrollIng = false;

//修正弹两次回调 蛋疼

callback this.tagName.toLowerCase()==’body’ callback();

});

}

};

//实例化

var directoryNav = new DirectoryNav($(“h2,h3”),{

scrollTopBorder:0   //滚动条距离顶部多少的时候显示导航,如果为0,则一直显示

});

求百度百科右侧底部js导航代码?高分悬赏

这其实用animate()方法+css的margin-top或margin-bottom就可以实现,关键是判断容器的高度

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/195413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-02 20:34
下一篇 2024-12-02 20:34

相关推荐

  • JS Proxy(array)用法介绍

    JS Proxy(array)可以说是ES6中非常重要的一个特性,它可以代理一个数组,监听数据变化并进行拦截、处理。在实际开发中,使用Proxy(array)可以方便地实现数据的监…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • 仓库管理系统代码设计Python

    这篇文章将详细探讨如何设计一个基于Python的仓库管理系统。 一、基本需求 在着手设计之前,我们首先需要确定仓库管理系统的基本需求。 我们可以将需求分为以下几个方面: 1、库存管…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • 写代码新手教程

    本文将从语言选择、学习方法、编码规范以及常见问题解答等多个方面,为编程新手提供实用、简明的教程。 一、语言选择 作为编程新手,选择一门编程语言是很关键的一步。以下是几个有代表性的编…

    编程 2025-04-29
  • Python实现简易心形代码

    在这个文章中,我们将会介绍如何用Python语言编写一个非常简单的代码来生成一个心形图案。我们将会从安装Python开始介绍,逐步深入了解如何实现这一任务。 一、安装Python …

    编程 2025-04-29
  • 怎么写不影响Python运行的长段代码

    在Python编程的过程中,我们不可避免地需要编写一些长段代码,包括函数、类、复杂的控制语句等等。在编写这些代码时,我们需要考虑代码可读性、易用性以及对Python运行性能的影响。…

    编程 2025-04-29
  • 北化教务管理系统介绍及开发代码示例

    本文将从多个方面对北化教务管理系统进行介绍及开发代码示例,帮助开发者更好地理解和应用该系统。 一、项目介绍 北化教务管理系统是一款针对高校学生和教职工的综合信息管理系统。系统实现的…

    编程 2025-04-29

发表回复

登录后才能评论