需求为一个简单的scroll效果,侧边选项卡跟随屏幕向下拖动变颜色的。点击侧边选项卡,跳转到相应模块。

索性上网找了一下类似的效果。附带源码地址  https://blog.csdn.net/dream_fa/article/details/72842193

原有样式与js确实有点欠妥,所以进行了简单修改。

需要注意的是js代码中。取得网页被卷去的高的时候,并未考虑兼容性的问题。以至于可能影响整体效果。

eg:// var scroH=document.body.scrollTop;

var scroH = document.documentElement.scrollTop || document.body.scrollTop;

实现后效果如下:

HTML代码如下

<section class="demo1">
里面是第一个
</section>
<section class="demo2">
里面是第2个
</section>
<section class="demo3">
里面是第3个
</section>
<section class="demo4">
里面是第4个
</section>
<section class="footer">
底部
</section>
<section class="menu">
<ul>
<li>
<a href="###"><i>1</i><label>第一个</label></a>
<a href="###"><i>2</i><label>第二个</label></a>
<a href="###"><i>3</i><label>第三个</label></a>
<a href="###"><i>4</i><label>第四个</label></a>
<a href="###"><i>back</i><label>返回顶部</label></a>
</li>
</ul>
</section>

CSS代码如下

header{width: 100%; height: 300px;}
.demo1, .demo2 ,.demo3, .demo4{width: 100%; height: 500px; text-align: center;}
.demo1{background-color: #00aaff;}
.demo2{background-color: #446622;}
.demo3{background-color: #779955;}
.demo4{background-color: #558844;}
/**这块根据自己项目来进行定位***/
.menu{width:200px;overflow: hidden;position: fixed; top: 50%; margin-top: -150px; background: #fff; right: 0px;display: block;}
.menu ul{ list-style: none; padding-left: 0px;}
.menu ul li{width: 100%; height: 100%;}
.menu ul li a{display: inline-block;width: 100%; height: 60px; line-height: 60px;color: #333} .menu ul li a i{display: inline-block;width:30px; height: 30px; float: left; text-align: center;line-height: 30px;
font-style:normal;background: #fff; margin:15px 15px;} .menu ul li a label{display: inline-block;width: 140px; height: 60px; float: right; text-align: center;line-height: 60px;cursor: pointer;} .menu ul li a.active{color: red}
.footer{background-color:#145214;width: 100%; height: 600px}

JS代码如下

<script src="jquery-1.12.3.js"></script>

<script>
$(function(){
// 获取菜单栏到顶部的距离
var navH=$(".menu").offset().top;
$(window).scroll(function(){
// 获取滚动条滑动距离
//让菜单栏距离顶部300时候显示默认隐藏,
// var scroH=document.body.scrollTop; var scroH = document.documentElement.scrollTop || document.body.scrollTop; //为了兼容性
console.log(top)
if(scroH>=300){
$(".menu").css({"display":"block"});
}else{
$(".menu").css({"display":"none"}) ;
}
if(scroH>=0 && scroH<500){
$(".menu ul li a:first-child").addClass("active").siblings().removeClass("active");
}else if(scroH>=500 && scroH<1000){
$(".menu ul li a:nth-child(2)").addClass("active").siblings().removeClass("active");
}else if(scroH>=1000 && scroH<1500){
$(".menu ul li a:nth-child(3)").addClass("active").siblings().removeClass("active");
}else if(scroH>=1500 && scroH<2000){
$(".menu ul li a:nth-child(4)").addClass("active").siblings().removeClass("active");
} })
$(".menu ul li a:first-child").click(function(){
$('body,html').animate({scrollTop:300},400);
})
$(".menu ul li a:nth-child(2)").click(function(){
$('body,html').animate({scrollTop:800},500);
})
$(".menu ul li a:nth-child(3)").click(function(){
$('body,html').animate({scrollTop:1300},500);
})
$(".menu ul li a:nth-child(4)").click(function(){
$('body,html').animate({scrollTop:1800},500);
})
$(".menu ul li a:last-child").click(function(){
$('body,html').animate({scrollTop:0},500);
})
})
</script>

关于scroll实现侧边导航栏的更多相关文章

  1. 【源码分享】jquery+css实现侧边导航栏

    jquery+css实现侧边导航栏 最近做项目的时候,突然想用一个侧边导航栏,网上找了几个插件,有的太丑而且不太符合我的预期.与其修改别人的代码,不如自己来写一个了.废话不多说先上图,感兴趣的请继续看 ...

  2. Android 新兴的UI模式——侧边导航栏【转】

    侧边导航栏也就是大家熟知的SliddingMenu,英文也叫Fly-In App Menu.Side Navigation等.当然谷歌现在已经推出类似这个效果的组件--Navigation Drawe ...

  3. [置顶] bootstrap自定义样式-bootstrap侧边导航栏的实现

    前言 bootstrap自带的响应式导航栏是向下滑动的,有时满足不了个性化的需求,需要做一个类似于android drawerLayout 侧滑的菜单,这就是我要实现的bootstrap自定义侧滑菜单 ...

  4. 【机器学习实践】Jupyter Notebook安装 侧边导航栏功能 操作及其他常用扩展功能介绍

    安装过程:  1. 首先我们引入jupyter_contrib_nbextension这个第三方库. 2. 在Anaconda Promot中输入命令: pip install jupyter_con ...

  5. html+css写出响应式侧边导航栏

    html部分:先写用div画好六个导航的卡片,再利用css添加响应效果 <div class='card-holder'> <div class='card-wrapper'> ...

  6. jQuery实现侧边导航栏效果

    效果图: 效果体验:http://keleyi.com/keleyi/phtml/jqmenu/2.htm 以下是完整代码: <!DOCTYPE html> <html> &l ...

  7. 使用bootstrap3.0搭建一个具有自定义风格的侧边导航栏

    由于工作变动,新的项目组,可能会涉及到更多的类似于后台管理系统这一类的项目,而且开发可能更加偏向于传统型的开发,希望今后能够在新的项目中能够用得上vuejs吧! 接手项目的时候,就是一个后台管理系统, ...

  8. iOS开发-仿大众点评iPad侧边导航栏

    昨天其实已经写了一篇侧边栏的文章,不过感觉还不是很清晰,这篇文章算是补充吧,iPad上看了大众点评的侧边栏,基本上百分之九十类似,具体效果可参考下图: 对比昨天主要做了两个修改,一个是图片和文字的显示 ...

  9. layui禁用侧边导航栏点击事件

    layui是一款优秀的前端模块化css框架,作者是贤心 —— 国内的一位前端大佬. 我用layui做过两个完整的项目,对她的感觉就是,这货非常适合做后台管理界面,且基于jquery,很容易上手.当然, ...

随机推荐

  1. python 数据压缩

    zlib 压缩 import zlib import this s = this.s.encode('utf8')*10 for i in range(10): data = zlib.compres ...

  2. es+mongodb 整合

    之前公司项目的数据都是从mysql查询,后面需求变更:同时技术上相应的也要改变策略,决定将mongodb和mysql的数据通过es建立索引来查询: 对于还没有接触或者真正了解es的可以先看一下相关Lu ...

  3. CF914D

    CF914D 用线段树乱搞一下就行qwq #include<iostream> #include<cstring> #include<cstdio> #includ ...

  4. OI中的快速傅里叶变换(FFT)

    快速傅里叶变换(FFT)                                                                               ---- LLpp ...

  5. enovia PLM: add characteristic to both prototype and product

    Issue: add new mandatory attribute named LUX_HazardousMaterial to protoype and product, and export t ...

  6. ios打包,通过Xcode生成ipa文件

    ios打包,通过Xcode生成ipa文件 干货文章 ·2018-03-21 19:03:47 打开ios项目目录,配置证书 将运行设备选择,如下图 选择:Product -> Scheme -& ...

  7. vue filters 日期

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 理解First-Class Functions

    def logger(msg): def log_message(): print('Log:', msg) return log_message # 返回的是函数 log_hi = logger(' ...

  9. Delphi 鼠标控制函数GetCursorPos、SetCursorPos

    GetCursorPos函数  获取鼠标的位置 var P: TPoint; begin GetCursorPos(P); //获取鼠标位置 end; SetCursorPos函数 设置鼠标的位置 v ...

  10. 【NOI2019模拟2019.6.29】字符串(SA|SAM+主席树)

    Description: 1<=n<=5e4 题解: 考虑\(f\)这个东西应该是怎样算的? 不妨建出SA,然后按height从大到小启发式合并,显然只有相邻的才可能成为最优答案.这样的只 ...