博主信息
博文 29
粉丝 0
评论 0
访问量 33204
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
jQuery实现鼠标移上去切换效果的选项卡
LIWEN的博客
原创
900人浏览过

功能实现思路:

首先在html中给选项卡菜单元素添加一个自定义属性item,并将item的值作为对应内容块的元素id,相当于将菜单和内容进行关联;

然后用jQuery选中当前菜单,为其设置选中样式,接下来选择其兄弟元素,移除选中样式;

同样,用jQuery选中与当前菜单对应的内容元素,为其移除隐藏样式,然后选中其兄弟元素,添加兄弟元素的隐藏样式。

注意此处有个小坑:内容元素们,需要一个盒子来包装一下,以防选中其兄弟元素时,把其他不相干元素也选中。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <title>jQuery选择器实现选项卡功能</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        h3 {
            color: #053482;
            font-size: 18px;
            font-weight: bold;
            display: table-cell;
            vertical-align: middle;
            height: 50px;
            width: 100%;
            padding-left: 15px;
        }
        #box {
            width: 600px;
            background-color: #EEEEEE;
            margin: 30px auto;
            position: relative;
        }
        .displayon {
            display: block;
        }
        #box div {
            width: 100%;
            background: #ccc;
            padding-bottom: 15px;
        }
        #box div ul.one {
            list-style: none;
            margin: 0;
            padding-top: 15px;
        }
        #box div ul.one li {
            list-style:inside disc;
            line-height: 30px;
            color: #053482;
        }
        #box div ul.one li a {
            display: table-cell;
            height: 30px;
        }
        #box div ul.one li a:link {
            font-size: 14px;
            text-decoration-line: none;
            color: #053482;
        }
        #box div ul.one li a:visited {
            color: #053482;
        }
        #box div ul.one li a:hover {
            color: #FFFFFF;
            background-color: #1466bc;
        }
        #box div ul.one li a:active {
            color: deepskyblue;
            background-color: #1466bc;
        }
        #div2 {
            width: 600px;
            background: #ccc;
            position: absolute;
            margin-top: 0;
            margin-left: 0;
        }
        ul.two {
            list-style: none;
            padding:10px;
        }
        ul.two li {
            margin: 0 auto;
            display: inline;
            float: left;
            padding: 8px;
            text-align: center;
        }
        ul.two img {
            border: solid 1px #FFFFFF;
            border-radius: 10px;
            margin-bottom: 5px;
            cursor: pointer;
            transition: all 0.6s;
        }
        ul.two img:hover{
            transform: scale(1.4);
        }
        ul.two li a {
            color: #053482;
            text-decoration-line: none;
        }
        ul.two li a:link {
            color: #053482;
            text-decoration-line: none;
        }
        ul.two li a:visited {
            color: #053482;
            text-decoration-line: none;
        }
        ul.two li a:hover {
            color: crimson;
            font-weight: bolder;
            font-size: 17px;
            text-decoration-line: none;
        }
        ul.two li a:active {
            color: darkred;
            text-decoration-line: none;
        }
        #div3 {
            width: 600px;
            position: absolute;
            margin-top: 0;
            margin-left: 0;
            margin-bottom: 15px;
            overflow: hidden;
        }
        #div3 h3 a:link{
            color: #053482;
            font-size: 18px;
            font-weight: bold;
            text-decoration-line: none;
        }
        #div3 h3 a:visited{
            color: #053482;
            font-size: 18px;
            font-weight: bold;
            text-decoration-line: none;
        }
        #div3 h3 a:hover{
            color: darkred;
            font-size: 18px;
            font-weight: bold;
            text-decoration-line: none;
        }
        #div3 img {
            float: left;
            margin-left: 20px;
            margin-right: 10px;
        }
        #div3 p {
            text-indent: 2em;
            line-height: 1.2em;
            font-family: "Times New Roman";
            color: #053482;
        }
        #box div ul {
            list-style: none;
            margin: 0;
            padding-top: 15px;
        }
        #box div ul li {
            list-style:inside disc;
            line-height: 30px;
            color: #053482;
        }
        #box input {
            font-size: 15px;
            padding: 7px 15px 7px 15px;
            border: 0;
        }
        .on {
            color: #fff;
            background: #a1b70d;
            font-size: 15px;
            padding: 7px 15px 7px 15px;
            outline: none;
        }
        hr {
            height:2px;
            border:none;
            border-top:2px dotted #185598;
            margin-top: 50px;
        }
        .hide {
            display: none;
        }


    </style>
    <script>

    </script>
</head>
<body>
<div id="box">
    <h3>常用资源</h3>
    <input item="div1" type="button" value="全文数据库" class="on" onmouseenter="tab(this);" >
    <input item="div2" type="button" value="主要期刊" onmouseenter="tab(this);">
    <input item="div3" type="button" value="文摘索引" onmouseenter="tab(this);">
    <input item="div4" type="button" value="会议论文" onmouseenter="tab(this);">
    <input item="div5" type="button" value="学位论文" onmouseenter="tab(this);">
    <div>
    <div id="div1" class="displayon">
        <ul class="one">
            <li><span><a href="">IOP</a></span></li>
            <li><span><a href="">Elsevier/ScienceDirect</a></span></li>
            <li><span><a href="">Springer</a></span></li>
            <li><span><a href="">IEL</a></span></li>
            <li><span><a href="">AIP Journals</a></span></li>
            <li><span><a href="">中国知网</a></span></li>
            <li><span><a href="">维普</a></span></li>
            <li><span><a href="">万方</a></span></li>
        </ul>
    </div>
    <div id="div2" class="hide">
        <ul class="two">
            <li><img src="images/shu1_library2017.jpg" height="231" width="172"/><br><a href="">ADS Service</a></li>
            <li><img src="images/shu2_library2017.jpg" height="231" width="172"/><br><a href="">Web of Science</a></li>
            <li><img src="images/shu3_library2017.jpg" height="231" width="172"/><br><a href="">EI工程索引</a></li>
        </ul>
    </div>
    <div id="div3" class="hide">
        <h3><a href="">Research in Astronomy and Astrophysics (RAA)</a></h3>
        <img src="images/raa.jpg" width="100"/>
        <p>RAA has moved our online peer review system to the ScholarOne (S1) system. Please re-register and submit your new submission to our new system, however current submissions will still stay in the OJS system until their review process is finished. We will also keep the OJS system as our publishing system.</p>
        <hr width="100%">
        <h3><a href="">Research in Astronomy and Astrophysics (RAA)</a></h3>
        <img src="images/raa.jpg" width="100"/>
        <p>RAA has moved our online peer review system to the ScholarOne (S1) system. Please re-register and submit your new submission to our new system, however current submissions will still stay in the OJS system until their review process is finished. We will also keep the OJS system as our publishing system.</p>

    </div>
    <div id="div4" class="hide">
        <ul>
            <li>Proceedings of IAU</li>
            <li>AIP Conference Proceedings</li>
            <li>Proceedings of ASP</li>
            <li>万方会议论文</li>
        </ul>
    </div>
    <div id="div5" class="hide">
        <ul>
            <li>PQDT-博硕士论文</li>
            <li>"美国博士论文"数据库</li>
            <li>ProQuest全球博硕士论文资料库</li>
            <li>CASTD中科院学位论文库</li>
            <li>中国知网(CNKI)科技类博硕士论文</li>
            <li>万方学位论文</li>
        </ul>
    </div>
</div>
</div>
<script>
    function tab(self) {
        $(self)
            .addClass("on")   //为当前选项卡添加选中样式
 .siblings()    //选中所有兄弟标签
 .removeClass("on");   //取消兄弟选项卡的on样式

 var box1 = "#" + $(self).attr("item")      //获取当前元素的自定义属性值
 $(box1)
            .removeClass("hide")   //移除当前标签的hide属性
 .siblings()     //选中所有兄弟标签
 .addClass("hide")   //给兄弟标签添加hide样式
 }
</script>
</body>
</html>


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学