导航栏nav案例(addEventListener事件监听)

定义和用法

addEventListener 用于向指定元素添加事件句柄(又称事件处理函数)

语法

element.addEventListener(event, function, useCapture)

element   文档节点,duocument,window。

event      必须有。是一个字符串,指定事件名(一般不使用“on前缀”,例如“click,mouseover”)。

function  必须有。指定要事件触发时执行的函数(可以是函数,也可以是函数名)。

useCapture 可选。布尔值(true 或者false)指定事件是否在捕获阶段还是在冒泡阶段执行,一般情况下默认值为false

1、如果为true则事件句柄在捕获阶段执行

2、如果为false则事件句柄在冒泡阶段执行

 

html:

<div class='row head_body_below'>
        <div class="col-md-1 col-sm-1 style_col_md">
            <a id='style_title_a' href=''>首页</a>
        </div>
        <div class="col-md-1 col-sm-1 style_col_md">
            <div class='title_style'>
                <a id='style_title_a' href=''> 院区概览</a>
            </div>
            <ul class='style_ul'>
                <li><a id='style_a' href=''>医院简介</a></li>
                <li><a id='style_a' href=''>院领导介绍</a></li>
                <li><a id='style_a' href=''>组织架构</a></li>
            </ul>
        </div>
        <div class="col-md-1 col-sm-1  style_col_md">
            <div class='title_style'>
                <a id='style_title_a' href=''> 科室导览</a>
            </div>
            <ul class='style_ul'>
                <li><a id='style_a' href=''>科室介绍</a></li>
                <li><a id='style_a' href=''>门诊时间</a></li>
            </ul>
        </div>
        <div class="col-md-1 col-sm-1  style_col_md">
            <div class='title_style'>
                <a id='style_title_a' href=''> 医院新闻</a>
            </div>
            <ul class='style_ul'>
                <li><a id='style_a' href=''>医院动态</a></li>
            </ul>
        </div>
        <div class="col-md-1 col-sm-1  style_col_md">
            <div class='title_style'>
                <a id='style_title_a' href='#'> 医师风采</a>
            </div>
            <ul class='style_ul'>
                <li><a id='style_a' href='expertteam.jsp?type=0'>医师风采</a></li>
                <li><a id='style_a' href='expertteam.jsp?type=1'>专家介绍</a></li>
                <li><a id='style_a' href='expertteam.jsp?type=2'>名医介绍</a></li>
            </ul>
        </div>
        <div class="col-md-1 col-sm-1 style_col_md">
            <div class='title_style border_style'>
                <a id='style_title_a' href='#'> 服务指南</a>
            </div>
            <ul class='style_ul'>
                <li><a id='style_a' href='#'>门诊指南</a></li>
                <li><a id='style_a' href='#'>住院指南</a></li>
                <li><a id='style_a' href='#'>交通指南</a></li>
                <li><a id='style_a' href='#'>体检指南</a></li>
            </ul>
        </div>
    </div>

css:

html{
    width: 100%;
    height: 100%;
}
body{
    width: 100%;
    height: 100%;
}
.style_col_md{
    font-size: 20px;
    color: #966334;
    text-align: center;
    padding: 0px;
    position: relative;
    width: 17%;
    height: 100px;
    line-height: 100px;
}
.head_body_below{
    text-align: center;
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 1200px;
    margin: 0 auto;
    height: 100px;
    background-color: #FFF8EB;
}
.title_style{
    height: 35px;
    padding: 0px;
    
}
.style_ul{
    list-style-type:none;
    padding: 0px;
    background-color: #FFF8EB;
    display: none;
    position: absolute;
    top:20px;
    width:90%;
    left:4px;
    z-index:99;
    margin-top: 80px;
    opacity: 0.8;

}
.style_ul li{
    line-height: 55px; 
    width: 100%;
}
#style_title_a{
    color:#956034;
    font-weight: bold;
    text-decoration: none;
}
#style_title_a:hover{
    color:#00A09D;
}
#style_hr{
    color:#C4A384;
}
.home_body_top{
    width: 100%;
}
#item_style_img{
    width:100%;
    height:100%;
}
#home_Gn{
    background-color: #FFF8EE;
    height: 200px;
    border-bottom: 2px solid #CCC6BD;
}
.home_style_img{
    width: 100%;
    height: 90%;
    text-align: center;
}
.home_style_img_title{
    width: 100%;
    height: 10%;
    text-align: center;
    margin-top: 10px;
}
#img_style_a{
    text-decoration: none;
    font-size: 18px;
}
#img_style_a:hover{
    color:#00A09D;
    
}
.foot_info_top a{
    color:white;
    text-decoration: none;
    font-size: 12px;
}
.foot_info_center{
    text-align:center;
    letter-spacing:2px;
    margin-top:10px; 
    color:white;
}
.style_border{
    background-color: #F4E8D8;
    height: 600px;
}
.style_span{
    font-weight: bold;
    font-size: 18px;
    color:#936235;
    margin-top: 20px;
    margin-left:20px;
    float: left;
}
.style_All_a{
    margin-top: 20px;
    font-size: 16px;
    text-align:right;
}
.style_All_a_style:hover{
    color: #936235;
    text-decoration: none;
}
.gn_style_img{
    width: 80%
}

.news{
    width:250px;
    height:230px;
}
.san{
    position: relative;
}

.gn_style_img{
    width: 50%;
}
#style_a{
    color: black;
    text-decoration: none;
    font-size: 16px;
    
}
#style_a:hover{
    color: #00A09D;
}

js:(需要先引入jq)

<script>
    $(function(){
        titleclick();
    })
    //导航栏移入移除
    function  titleclick(){
            var sc = document.getElementsByClassName('style_col_md');
            for (var i = 0; i < sc.length; i++) {
                sc[i].addEventListener("mouseenter",function(){
                    $(this).children("ul").css("display","block");
                })
                sc[i].addEventListener("mouseleave",function(){
                    $(this).children("ul").css("display","none");
                 });
            }
        }
</script>

 

转载于:https://www.cnblogs.com/fanting/p/9401288.html

HTML5横向滑动导航栏可以通过CSS和JavaScript来实现。 首先,在HTML文件中创建一个`<nav>`标签,并设置一个id属性,例如`id="horizontal-nav"`。在`<nav>`标签中,再创建一个无序列表`<ul>`,作为导航栏的容器。在`<ul>`标签中,创建多个列表项`<li>`,每个列表项表示导航栏的一个选项。每个列表项可以包含一个`<a>`标签,通过 href 属性设置跳转链接,并在标签内添加相应的文本内容。 接下来,使用CSS来设置导航栏的样式。通过选择器选择`#horizontal-nav`来设置导航栏的宽度、高度、背景颜色等样式。可以设置`display: flex`来使导航栏中的选项横向排列。通过选择器选择`#horizontal-nav li`来设置每个选项的样式,可以设置`list-style: none`来去除默认的列表样式,设置`padding`来调整选项之间的间距。 最后,使用JavaScript来实现导航栏的横向滑动效果。首先,为导航栏容器`<ul>`添加一个事件监听器,例如使用`addEventListener("mousemove", scrollNav)`来监听鼠标移动事件。在事件处理函数`scrollNav`中,可以通过计算鼠标相对于导航栏容器的位置,来改变导航栏容器的滚动位置,从而实现滑动效果。可以使用`scrollLeft`属性来设置导航栏容器的滚动位置。 通过以上的步骤,就可以实现HTML5横向滑动导航栏。可以根据实际需求,进一步添加样式、动画效果、响应式设计等来完善导航栏的功能和外观。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值