12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <!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>
- .box div {
- width: 200px;
- height: 120px;
- background-color: #aaa;
- color: #fff;
- font-size: 40px;
- font-weight: bolder;
- text-align: center;
- line-height: 120px;
- }
- .box {
- display: flex;
- }
- .box .active {
- background-color: #111;
- }
- </style>
- </head>
- <body>
- <div class="box">
- <div data-val="first" class="tab-btn active">first</div>
- <div data-val="second" class="tab-btn">second</div>
- <div data-val="third" class="tab-btn">third</div>
- </div>
- <script>
- var tabBtns = document.querySelectorAll('.tab-btn');
- tabBtns.forEach((item)=>{
- item.onclick = function(){
- tabBtns.forEach((item)=>{
- item.classList.remove("active");
- });
- this.classList.add("active");
- let thisVal = this.dataset.val;
- window.history.pushState(thisVal,"");
- }
- })
- window.onpopstate = function(){
- let thisState = window.history.state;
- tabBtns.forEach((item)=>{
- if(item.dataset.val == thisState){
- tabBtns.forEach((item)=>{
- item.classList.remove("active");
- });
- item.classList.add("active");
- }
- });
- }
- </script>
- </body>
- </html>
|