首页 > 其他分享 >JS 选项卡

JS 选项卡

时间:2022-11-18 21:36:07浏览次数:48  
标签:box 选项卡 bottom 标题 height spans div JS

思路:
需要标题和页面内容两个部分,几个标题就对应几个页面,页面设置定位使其重叠,在美化一下css样式即可
当点击某个标题时,显示出与标题相关的内容,同时标题样式发生变化,然后关联需要展示的页面内容,切换标题的同时页面同时切换
1、获取元素;
2、for循环按钮元素添加onclick(点击) 或者 onm ousemove(移入)事件,这里用的是onclick事件;
3、点击标题时会有背景颜色,通过for循环实现当指向哪一个标题时给它添加背景,并且让其他标题的背景属性为空。
4、点击当前标题时同时展示内容。
HTML部分:

点击查看代码
<div class="box">
        <div class="box_top">
            <span>1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
        </div>
        <div class="box_bottom">
            <div><img src="./images/1.jpg" alt=""></div>
            <div><img src="./images/2.jpg" alt=""></div>
            <div><img src="./images/3.jpg" alt=""></div>
            <div><img src="./images/5.jpg" alt=""></div>
        </div>
    </div>

CSS部分:

点击查看代码
 * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 600px;
            height: 400px;
            border: 1px solid #000;
            margin: 50px auto;
        }

        .box .box_top {
            width: 100%;
            height: 60px;
            background: rgb(168, 203, 246);
            display: flex;
            text-align: center;
            line-height: 60px;
        }

        .box .box_top span {
            flex: 1;
            cursor: pointer;
        }

        .box .box_bottom {
            height: 540px;
            width: 100%;
            background: rgb(178, 216, 237);
            position: relative;
        }

        .bgc {
            background: rgb(226, 16, 16);
            color: #fff;
        }

        .box .box_bottom div {
            height: 540px;
            width: 100%;
            background: rgb(230, 132, 71);
            font-size: 100px;
            color: #fff;
            position: absolute;
            top: 0;
            left: 0;
            display: none;
        }

        .box .box_bottom div img {
            width: 100%;
            height: 100%;
        }

        .box .box_bottom div.show {
            display: block;
        }

JS部分:

点击查看代码
 <script>
        onl oad = function () {
            //获取元素
            var box = document.querySelector('.box')
            var spans = box.querySelectorAll('span')
            var divs = box.querySelector('.box_bottom').querySelectorAll('div')
            console.log(divs);
            // 标题切换

            for (var i = 0; i < spans.length; i++) {
                // 给每个span自定义一个叫做index的属性,值是i,作用是用来作为连接box_bottom的div元素
                spans[i].setAttribute('index', i)
                spans[i].onclick = function () {
                    for (var j = 0; j < spans.length; j++) {
                        // 当指向哪一个span时给它添加背景,并且让其他span的背景属性为空
                        spans[j].className = ''
                        divs[j].className = ''
                    }
                    this.className = 'bgc'
                    //切换内容
                    // 获得每次this指向哪一个span时代表的值,将其转为数值类型
                    var index = this.getAttribute('index') - 0
                    // 当前的div设置一个show的类型
                    divs[index].className = 'show'
                }
            }

        }
    </script>

标签:box,选项卡,bottom,标题,height,spans,div,JS
From: https://www.cnblogs.com/qianfanqf/p/16904933.html

相关文章

  • nodejs.01
    fs模块导入fs模块,来操作文件constfs=require('fs');调用fs.readFile()方法读取文件参数1:读取文件存放路径参数2:读取文件时候采用的编码格式,一般默认utf-8参......
  • C#利用js脚本实现配置的文本表达式计算
    usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingMSScriptControl;namespaceMyQuery.CSharpScript{  ///......
  • 二级联动 js脚本
    //二级联动说明:联动的select的值必须匹配allinfo为二级所有值的数组格式为value|text  //贾世义functionselectChange(obj,changeId,allinfo){   varselV......
  • Nodejs报错记录
    1.digitalenveloperoutines::unsupportedD:\workspace\vuedemo>npmrundev...error:0308010C:digitalenveloperoutines::unsupportedatnewHash(node:int......
  • .HttpMessageNotReadableException: JSON parse error: Cannot
    报错代码org.springframework.http.converter.HttpMessageNotReadableException:JSONparseerror:Cannotdeserializeinstanceofjava.util.LinkedHashMapoutofST......
  • xml解析_Jsoup_Document对象、Element对象
    xml解析_Jsoup_Document对象Document:文档对象,代表内存中的dom树获取Element对象getElementById(Stringid):根据id属性值获取唯一的elemtnt对象ge......
  • vue 3 打印 print-js
     1、安装npminstallprint-js--save2、引用importprintfrom'print-js'3、编写打印函数constenterDialog=async()=>{conststyle='@......
  • 求超大文件上传方案( jsp )
    ​ javaweb上传文件上传文件的jsp中的部分上传文件同样可以使用form表单向后端发请求,也可以使用ajax向后端发请求    1.通过form表单向后端发送请求     ......
  • vue3 生成二维码 qrcodejs2-fix
    1、安装qrcodejs2-fixnpminstallqrcodejs2-fix2、引入qrcodejs2-fiximportQRCodefrom'qrcodejs2-fix';3、在页面中定义容器<divid="code"></div>4、定义......
  • 2022-11-18 js 获取当前时间 格式为“yyyy-MM-dd HH:MM:SS”
    //获取当前时间格式为“yyyy-MM-ddHH:MM:SS”getNowFormatDate(){vardate=newDate();varseperator1="-";varseperator2......