search
HomeWeb Front-endJS TutorialNative js realizes e-commerce side navigation effect

知识要点

实现原理:

1.点击楼层跳相应楼层,用的是锚点定位

电梯:2F 个护家清

列表title:

这个没什么好说的

2.当点击楼层跳到相应楼层时,该楼层高亮显示

我们的脚本主要就是实现这个功能。

原理获取浏览器导航条下拉的距离与各个楼层相对于文档顶部下拉的距离进行校验。

这里需要用到的方法事件:

window.onscroll=function(){ } //浏览器滚动监听事件执行函数
.scrollTop //获取滚动条下拉的高度,大家可以用 console.log(top) 试一下就明白了
.offsetTop //获取元素距离文档顶部的距离

   

大体过程分析

首先获取滚动条下拉的高度,以及存储一些会用到的变量

//获取滚动条下拉的高度
var top=document.documentElement.scrollTop||document.body.scrollTop;
//console.log(top)
//所有楼层
var items=document.getElementById("wrap").getElementsByClassName("floor-title");
//a标签父级
var elev=document.getElementById("elev");

   

遍历所有楼层,获取每个楼层距离文档顶部的距离,如果滚动条的高度大于楼层的高度,就把楼层的ID赋给thisID,

每个楼层都比较一次,直到不满足条件退出循环。

//创建空变量下面存储当前楼层的ID
var thisID="";
//遍历所有楼层
for(var i=0;i<items.length;i++){
 //获取每个楼层距离文档顶部的距离
 var itemTop=items[i].offsetTop;
 //console.log(itemTop)
 //如果滚动条的高度大于楼层的高度,就把楼层的ID赋给thisID
 //每个楼层都比较一次,直到不满足条件退出循环
 if(top>itemTop-100){//减去100是为了用户体验,自己测试下就懂了
 thisID=items[i].id;
 }else{
 break;
 } 
}

   

最后就是给当前楼层添加高亮样式,其他的删除样式。

因为用的是原生js,没有jquery强大的选择器所以还要用一个循环遍历获取href值是当前楼层的ID,

这里需要注意的是,在js里 .href  获取的是个完整的链接,所以要用split()方法分割为数组,数组的最后一位就是ID了

//所有a标签
var alinks=elev.getElementsByTagName("a");
if(thisID){
 for(var j=0;j<alinks.length;j++){
 //因为获取的是一个完整链接所以要切割两半
 var _href=alinks[j].href.split("#");
 //对数组最后一位和当前高度楼层的id进行校验
 if(_href[_href.length-1]!=thisID){
 alinks[j].className="";
 }else{
 addClass(alinks[j],"current")
 }
 }
}

   

完整代码

注:图片链接自己替换下,再多复制些商品列表让浏览器足以满屏

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
body{background: #9B1BC5;}
@font-face {
 font-family: &#39;iconfont&#39;; /* project id 196174 */
 src: url(&#39;https://at.alicdn.com/t/font_9dwvwg9edp0q9f6r.eot&#39;);
 src: url(&#39;https://at.alicdn.com/t/font_9dwvwg9edp0q9f6r.eot?#iefix&#39;) format(&#39;embedded-opentype&#39;),
 url(&#39;https://at.alicdn.com/t/font_9dwvwg9edp0q9f6r.woff&#39;) format(&#39;woff&#39;),
 url(&#39;https://at.alicdn.com/t/font_9dwvwg9edp0q9f6r.ttf&#39;) format(&#39;truetype&#39;),
 url(&#39;https://at.alicdn.com/t/font_9dwvwg9edp0q9f6r.svg#iconfont&#39;) format(&#39;svg&#39;);
}
.cart{
 font-family:"iconfont" !important;
 font-size:36px;font-style:normal;
 -webkit-font-smoothing: antialiased;
 -webkit-text-stroke-width: 0.2px;
 -moz-osx-font-smoothing: grayscale;
 cursor: pointer;
 color: #dd2727;
 height: 36px;
 width: 36px;
 padding: 0;
 line-height: 1;
 position: absolute;
 right: 0;
 bottom: 0;
}
.floor-title{width: 990px; margin: 0 auto; overflow: hidden;}
.floor-text{width:990px; height: 80px; margin: 10px auto 0; font-size: 100%;}
.content{width: 990px; margin: 0 auto; overflow: hidden;}
.list0{font-size: 0; margin: 0 -10px -10px 0;}
.item{display: inline-block; vertical-align: top; width: 190px; margin:0 10px 10px 0; background: #fff; position: relative; line-height: 1.2;}
.item a{display: block;}
.item-main{padding: 9px; position: relative;}
.item-title{font: 14px/1.3 tahoma,arial,"\5b8b\4f53"; color: #313D44; display: -webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; overflow: hidden; margin-bottom: 3px;}
.item-desc{font: 12px/1.2 tahoma,arial,"\5b8b\4f53"; color: #dd2727; margin-bottom: 5px;}
.item-imp{font: 12px/1.2 tahoma,arial,"\5b8b\4f53"; margin-top: 9px; padding-right: 35px; position: relative;}
.item-sales{color: #666;}
.promotion-price{font-size: 18px; color: #dd2727; font-weight: 700; font-family: Helvetica; line-height: 1.1; display: inline-block;}
.promotion-price:first-letter {font-size: 14px; font-weight: 400; margin-right: 1px;}
.elevator{position: fixed; left: 50%; margin-left: 505px; bottom: 60px; width: 100px; font: 12px/1.5 "Microsoft Yahei",tahoma,arial;}
.elevator ul{background: #B50100;}
.elevator ul li a{height: 25px; line-height: 25px; color: #fff; display: block; text-align: center;}
.elevator ul li a:hover,
.elevator ul li a.current{background: #800100;}
</style>
</head>
<body>
 <div class="elevator" id="elev">
 <ul>
 <li><a href="#item1" class="current">1F 休闲食品</a></li>
 <li><a href="#item2">2F 个护家清</a></li>
 <li><a href="#item3">3F 粮油干货</a></li>
 <li><a href="#item4">4F 母婴用品</a></li>
 <li><a href="#item5">5F 进口食品</a></li>
 <li><a href="#item6">6F 纸品日百</a></li>
 </ul>
 </div>
 <div id="wrap">
 <div class="floor-title" id="item1"><h3 class="floor-text"><img  src="/static/imghwm/default1.png"  data-src="images/1f.png"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></h3></div>
 <div class="content">
 <div class="list0">
 <div class="item">
 <a href="#">
 <div class="img-wrap"><img  src="/static/imghwm/default1.png"  data-src="images/pic.jpg"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></div>
 <div class="item-main">
 <div class="item-info">
 <div class="item-title">Walch/威露士清新青柠水润保湿洁净健康沐浴露600mlssssssssssss</div>
 <div class="item-desc">满188减100</div>
 </div>
 <div class="item-imp">
 <div class="imp-main">
  <div class="item-sales">月销25170件</div>
  <div class="item-price"><b class="promotion-price">¥29.9</b></div>
 </div>
 <i class="cart"></i>
 </div>
 </div>
 </a>
 </div>
 </div>
 </div>
 <div class="floor-title" id="item2"><h3 class="floor-text"><img  src="/static/imghwm/default1.png"  data-src="images/2f.png"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></h3></div>
 <div class="content">
 <div class="list0">
 <div class="item">
 <a href="#">
 <div class="img-wrap"><img  src="/static/imghwm/default1.png"  data-src="images/pic.jpg"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></div>
 <div class="item-main">
 <div class="item-info">
 <div class="item-title">Walch/威露士清新青柠水润保湿洁净健康沐浴露600mlssssssssssss</div>
 <div class="item-desc">满188减100</div>
 </div>
 <div class="item-imp">
 <div class="imp-main">
  <div class="item-sales">月销25170件</div>
  <div class="item-price"><b class="promotion-price">¥29.9</b></div>
 </div>
 <i class="cart"></i>
 </div>
 </div>
 </a>
 </div>
 </div>
 </div>
 <div class="floor-title" id="item3"><h3 class="floor-text"><img  src="/static/imghwm/default1.png"  data-src="images/3f.png"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></h3></div>
 <div class="content">
 <div class="list0">
 <div class="item">
 <a href="#">
 <div class="img-wrap"><img  src="/static/imghwm/default1.png"  data-src="images/pic.jpg"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></div>
 <div class="item-main">
 <div class="item-info">
 <div class="item-title">Walch/威露士清新青柠水润保湿洁净健康沐浴露600mlssssssssssss</div>
 <div class="item-desc">满188减100</div>
 </div>
 <div class="item-imp">
 <div class="imp-main">
  <div class="item-sales">月销25170件</div>
  <div class="item-price"><b class="promotion-price">¥29.9</b></div>
 </div>
 <i class="cart"></i>
 </div>
 </div>
 </a>
 </div>
 </div>
 </div>
 <div class="floor-title" id="item4"><h3 class="floor-text"><img  src="/static/imghwm/default1.png"  data-src="images/4f.png"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></h3></div>
 <div class="content">
 <div class="list0">
 <div class="item">
 <a href="#">
 <div class="img-wrap"><img  src="/static/imghwm/default1.png"  data-src="images/pic.jpg"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></div>
 <div class="item-main">
 <div class="item-info">
 <div class="item-title">Walch/威露士清新青柠水润保湿洁净健康沐浴露600mlssssssssssss</div>
 <div class="item-desc">满188减100</div>
 </div>
 <div class="item-imp">
 <div class="imp-main">
  <div class="item-sales">月销25170件</div>
  <div class="item-price"><b class="promotion-price">¥29.9</b></div>
 </div>
 <i class="cart"></i>
 </div>
 </div>
 </a>
 </div>
 </div>
 </div>
 <div class="floor-title" id="item5"><h3 class="floor-text"><img  src="/static/imghwm/default1.png"  data-src="images/5f.png"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></h3></div>
 <div class="content">
 <div class="list0">
 <div class="item">
 <a href="#">
 <div class="img-wrap"><img  src="/static/imghwm/default1.png"  data-src="images/pic.jpg"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></div>
 <div class="item-main">
 <div class="item-info">
 <div class="item-title">Walch/威露士清新青柠水润保湿洁净健康沐浴露600mlssssssssssss</div>
 <div class="item-desc">满188减100</div>
 </div>
 <div class="item-imp">
 <div class="imp-main">
  <div class="item-sales">月销25170件</div>
  <div class="item-price"><b class="promotion-price">¥29.9</b></div>
 </div>
 <i class="cart"></i>
 </div>
 </div>
 </a>
 </div>
 </div>
 </div>
 <div class="floor-title" id="item6"><h3 class="floor-text"><img  src="/static/imghwm/default1.png"  data-src="images/6f.png"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></h3></div>
 <div class="content">
 <div class="list0">
 <div class="item">
 <a href="#">
 <div class="img-wrap"><img  src="/static/imghwm/default1.png"  data-src="images/pic.jpg"  class="lazy"   alt="Native js realizes e-commerce side navigation effect" ></div>
 <div class="item-main">
 <div class="item-info">
 <div class="item-title">Walch/威露士清新青柠水润保湿洁净健康沐浴露600mlssssssssssss</div>
 <div class="item-desc">满188减100</div>
 </div>
 <div class="item-imp">
 <div class="imp-main">
  <div class="item-sales">月销25170件</div>
  <div class="item-price"><b class="promotion-price">¥29.9</b></div>
 </div>
 <i class="cart"></i>
 </div>
 </div>
 </a>
 </div>
 </div>
 </div>
 </div>
 <script type="text/javascript">
 //在页面加载完后立即执行多个函数完美方案。
 function addloadEvent(func){
 var oldonload=window.onload;
 if(typeof window.onload !="function"){
  window.onload=func;
 }
 else{
  window.onload=function(){
  if(oldonload){
   oldonload();
  }
  func();
  }
 }
 }
 addloadEvent(b);
 //在页面加载完后立即执行多个函数完美方案over。
 //给元素在原来基础上添加一个className
 function addClass(element,value){
 if(!element.className){
 element.className=value;
 }
 else{
 newClassName=element.className;
 newClassName+=" ";
 newClassName+=value;
 element.className=newClassName;
 }
 }
 function b(){
 window.onscroll=function(){
 //获取滚动条下拉的高度
 var top=document.documentElement.scrollTop||document.body.scrollTop;
 //console.log(top)
 //所有楼层
 var items=document.getElementById("wrap").getElementsByClassName("floor-title");
 //a标签父级
 var elev=document.getElementById("elev");
 //创建空变量下面存储当前楼层的ID
 var thisID="";
 //遍历所有楼层
 for(var i=0;i<items.length;i++){
 //获取每个楼层距离文档顶部的距离
 var itemTop=items[i].offsetTop;
 //console.log(itemTop)
 //如果滚动条的高度大于楼层的高度,就把楼层的ID赋给thisID
 //每个楼层都比较一次,直到不满足条件退出循环
 if(top>itemTop-100){//减去100是为了用户体验,自己测试下就懂了
 thisID=items[i].id;
 }else{
 break;
 } 
 }
 //所有a标签
 var alinks=elev.getElementsByTagName("a");
 if(thisID){
 for(var j=0;j<alinks.length;j++){
 //因为获取的是一个完整链接所以要切割两半
 var _href=alinks[j].href.split("#");
 //对数组最后一位和当前高度楼层的id进行校验
 if(_href[_href.length-1]!=thisID){
 alinks[j].className="";
 }else{
 addClass(alinks[j],"current")
 }
 
 }
 }
 }
 }
 </script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持PHP中文网!

更多原生js实现电商侧边导航效果相关文章请关注PHP中文网!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!