| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- }
- li {
- list-style: none;
- }
- .box {
- width: 600px;
- height: 100px;
- margin: 100px auto;
- }
- .box ul {
- display: flex;
- }
- .box li {
- width: 200px;
- height: 100px;
- background-color: #aaa;
- font-size: 50px;
- text-align: center;
- line-height: 100px;
- color: #fff;
- }
- .box .active {
- background-color: #111;
- }
- </style>
- </head>
- <body>
- <div class="box">
- <ul>
- <li class="first active">first</li>
- <li class="second">second</li>
- <li class="third">third</li>
- </ul>
- </div>
- <script>
- var aLi = document.querySelectorAll("li");
- for (var i = 0; i < aLi.length; i++) {
- aLi[i].onclick = function () {
- var innerText = this.innerText;
- window.location.hash = innerText;
- for (var j = 0; j < aLi.length; j++) {
- aLi[j].classList.remove("active");
- }
- this.classList.add("active");
- }
- }
- window.onhashchange = function(){
- var hash = window.location.hash;
- hash = hash.slice(1);
- console.log(hash);
- if(hash == ""){
- for(var j=0;j<aLi.length;j++){
- aLi[j].classList.remove("active");
- }
- aLi[0].classList.add("active");
- return;
- }
- for(var i=0;i<aLi.length;i++){
- if(aLi[i].innerText == hash){
- aLi[i].classList.add("active");
- }else{
- aLi[i].classList.remove("active");
- }
- }
- }
- </script>
- </body>
- </html>
|