原生js造轮子 发表于 2017-05-16 | 分类于 JavaScript tab切换代码 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width-device-width,initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1"> <title>JS Bin</title> <style> * { box-sizing: border-box; } .clearfix:after { content: ''; display:block; clear: both; } ul, li { list-style: none; margin: 0; padding: 0; } #tab { width: 600px; border: 1px solid #ccc; border-radius: 5px; margin: 0 auto; margin-top: 20px; } .tab-header > ul > li { float: left; padding: 10px 20px; border: 1px solid #ccc; font-size: 20px; cursor: pointer; } .tab-header > ul > .active { background-color: #ccc; } .tab-content > ul > li { height: 400px; display:none; } .tab-content> ul > li > img { width: 100%; height: 100%; } .tab-content > ul > .current { display: block } </style></head><body> <section id="tab"> <div class="tab-header"> <ul class="clearfix"> <li class="active">狗狗</li> <li>猫咪</li> <li>兔子</li> </ul> </div> <div class="tab-content"> <ul> <li class="current"><img src="http://oqev4hx8u.bkt.clouddn.com/js-demo-1.png" alt=""></li> <li><img src="http://oqev4hx8u.bkt.clouddn.com/js-demo-2.png" alt=""></li> <li><img src="http://oqev4hx8u.bkt.clouddn.com/js-demo-3.png" alt=""></li> </ul> </div> </section> <script> function $(str){ return document.querySelector(str); } var tabHeader = $('.tab-header > ul'); var tabCt = $('.tab-content > ul'); tabHeader.addEventListener('click',function(e){ if(e.target.tagName.toLowerCase() === 'li'){ for(var i = 0;i < tabHeader.children.length;i++){ tabHeader.children[i].classList.remove('active'); } e.target.classList.add('active'); for(var i = 0;i < tabCt.children.length;i++){ tabCt.children[i].classList.remove('current'); } var index = [].indexOf.call(tabHeader.children,e.target); // 将点击的导航li的序号存到变量index tabCt.children[index].classList.add('current'); // 根据变量index来对应的给图片li添加class } }) </script></body></html> 效果 模态框代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width-device-width,initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1"> <style> .clearfix:after { content: ''; display: block; clear: both; } #btn { cursor: pointer; } .panel { position: absolute; width: 300px; top: 50%; left: 50%; transform: translate(-50%,-50%); padding: 10px; border: 1px solid; border-radius: 5px; background-color: #fff; display: none; z-index: 2; } .panel .close { float: right; margin-right: 5px; font-size: 24px; cursor: pointer; } .panel .sure, .panel .cancel { float: right; border: 1px solid; border-radius: 5px; padding: 5px; cursor: pointer; } .shadow { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: #ccc; opacity: 0.7; z-index: 1; display: none; } </style> <title>JS Bin</title></head><body> <button id="btn">点我</button> <div class="panel clearfix"> <div class="close">X</div> <h1>我是一级标题</h1> <p>我是一大段文字</p> <div class="cancel">取消</div> <div class="sure">确定</div> </div> <div class="shadow"></div> <script> function $(str){ return document.querySelector(str); } var panel = $(".panel"); var btn = $("#btn"); var close = $(".panel>.close") var shadow = $(".shadow") btn.addEventListener("click", function(e){ e.stopPropagation(); panel.style.display = "block"; shadow.style.display = "block"; }) close.addEventListener("click",function(){ panel.style.display = "none"; shadow.style.display = "none"; }) panel.addEventListener("click",function(e){ e.stopPropagation(); }) window.addEventListener("click",function() { panel.style.display = "none"; shadow.style.display = "none"; }) </script></body></html>