js导航条

<!--
 所学技术:
    1.使用定时器延迟调用函数 和清除定时器
    2.style.cursor = cursor;手型光标
    3.visibility属性   
    4.border-right: none;设置边框
    5.offsetHeight 对象的长
    6.offsetTop 对象的高度
    7.absolute属性 绝对坐标
    -->


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>js 简单下拉菜单效果</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<meta name="description" content="" />
<meta name="keywords"    content="" />

<script type="text/javascript" >

    function at_show_aux(parent, child) {//子导航位置
        var p = document.getElementById(parent);
        var c = document.getElementById(child);

        //子导航出现在母导航的下面还是右边
        var top = (c["at_position"] == "y") ? p.offsetHeight + 2 : 0;//母导航高度+距离
        var left = (c["at_position"] == "x") ? p.offsetWidth + 2 : 0;

       
            top += p.offsetTop;//母导航高度+距离+母导航位置
            left += p.offsetLeft;
        

        c.style.position = "absolute";
        c.style.top = top + 'px';
        c.style.left = left + 'px';
        c.style.visibility = "visible";
    }

    // ***** at_show *****

    function at_show() {
        var p = document.getElementById(this["at_parent"]);
        var c = document.getElementById(this["at_child"]);

        at_show_aux(p.id, c.id);
        clearTimeout(c["at_timeout"]);//清除定时器
    }

    // ***** at_hide *****

    function at_hide() {

        var c = document.getElementById(this["at_child"]);

        c["at_timeout"] = setTimeout("document.getElementById('" + c.id + "').style.visibility = 'hidden'", 100);//设定100毫秒后执行
    }

    // ***** at_click *****



    function at_attach(parent, child,  position, cursor) {
        var p = document.getElementById(parent);//母导航
        var c = document.getElementById(child);//子导航

        p["at_parent"] = p.id;//母导航 
        p["at_child"] = c.id;//母导航下的子导航
        p["at_position"] = position;//导航位置

        c["at_parent"] = p.id;//子导航上的母导航
        c["at_child"] = c.id;//子导航
        c["at_position"] = position;//导航位置

        c.style.position = "absolute";
        c.style.visibility = "hidden";//隐藏子导航

        if (cursor != undefined) {
            p.style.cursor = cursor;//手型光标
            c.style.cursor = cursor;
        }


                p.onmouseover = at_show;
                p.onmouseout = at_hide;
                c.onmouseover = at_show;
                c.onmouseout = at_hide;


    }
</script>

<style type="text/css">

body { font-size: 0.7em; }
h3   { font-size: 1.6em; margin: 0px; }

a.sample_attach, div.sample_attach
{
  display: block;
  width:   100px;

  border:  1px solid black;
  padding: 2px 5px;

  background: #FFFFEE;

  text-decoration: none;
  font-family: Verdana, Sans-Sherif;
  font-weight: 900;
  font-size: 1.0em;
  color:   #008000;
  FLOAT: none;/*如果是横向导航条 就是left  纵向的就是none*/
}


a.sample_attach { border-right: none; }
div#sample_attach_menu_child             { border-right: 1px solid black; }
div#sample_attach_menu_child2             { border-right: 1px solid black; }
div#sample_attach_menu_child3             { border-right: 1px solid black; }



</style>

</head>
<body>



<div id="sample_attach_menu_parent" class="sample_attach">Main Menu</div>
<div id="sample_attach_menu_child" >
<a class="sample_attach">Item 1</a>
<a class="sample_attach">Item 2</a>
<a class="sample_attach">Item 3</a>
</div>
<div id="sample_attach_menu_parent2" class="sample_attach">Main Menu</div>
<div id="sample_attach_menu_child2" >
<a class="sample_attach">Item 1</a>
<a class="sample_attach">Item 2</a>
<a class="sample_attach">Item 3</a>
</div>

<div id="sample_attach_menu_parent3" class="sample_attach">Main Menu</div>
<div id="sample_attach_menu_child3" >
<a class="sample_attach">Item 1</a>
<a class="sample_attach">Item 2</a>
<a class="sample_attach">Item 3</a>
</div>
<script type="text/javascript">
    at_attach("sample_attach_menu_parent", "sample_attach_menu_child", "x", "pointer");//如果是横向就是y,纵向的事x
    at_attach("sample_attach_menu_parent2", "sample_attach_menu_child2",  "x", "pointer");
    at_attach("sample_attach_menu_parent3", "sample_attach_menu_child3",  "x", "pointer");
</script>







</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
百度百科右侧导航条js 实现主要包括以下几个方面: 1. 使用 position: fixed 实现固定在页面右侧位置; 2. 使用 JavaScript 监听滚动事件,根据滚动位置判断显示/隐藏状态; 3. 使用 JavaScript 动态改变导航条的样式和内容。 具体实现步骤如下: 1. 在 HTML 中添加导航条的代码,包括导航项的链接和图标等; 2. 使用 CSS 给导航条设置样式,包括位置、大小、颜色、背景等; 3. 使用 JavaScript 监听页面滚动事件,根据页面滚动位置计算导航条的显示/隐藏状态; 4. 使用 JavaScript 修改导航条的样式和内容,包括当前选中项的样式、滚动到指定位置的动画效果等。 以下是示例代码: HTML 代码: ```html <div class="nav"> <a href="#section1"><i class="icon1"></i> Section 1</a> <a href="#section2"><i class="icon2"></i> Section 2</a> <a href="#section3"><i class="icon3"></i> Section 3</a> </div> ``` CSS 代码: ```css .nav { position: fixed; top: 50%; right: 0; transform: translateY(-50%); } .nav a { display: block; width: 40px; height: 40px; line-height: 40px; text-align: center; color: #666; background: #fff; border: 1px solid #ccc; border-radius: 50%; margin-bottom: 20px; transition: all 0.3s; } .nav a:hover { color: #fff; background: #333; border-color: #333; } .nav a.active { color: #fff; background: #333; border-color: #333; } ``` JavaScript 代码: ```javascript var nav = document.querySelector('.nav'); var links = nav.querySelectorAll('a'); var sections = []; links.forEach(function(link) { var section = document.querySelector(link.getAttribute('href')); sections.push(section); }); window.addEventListener('scroll', function() { var currentSectionIndex = 0; sections.forEach(function(section, index) { if (section.offsetTop - window.innerHeight / 2 <= window.scrollY) { currentSectionIndex = index; } }); links.forEach(function(link, index) { if (index === currentSectionIndex) { link.classList.add('active'); } else { link.classList.remove('active'); } }); }); ``` 以上代码实现了一个简单的固定在页面右侧的导航条,可以根据页面滚动位置自动高亮当前所在的部分。你可以根据需要修改样式和代码逻辑,实现更复杂的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值