点击垂直导航栏中的某项实现页面跳转CSS+JavaScript

目录

1.主要页面

 2.相关部分的介绍

2.1 首先整体的构思

2.2 相关代码的解释

源代码如下:

希望能够帮到你哦~


1.主要页面

 2.相关部分的介绍

2.1 首先整体的构思

这里用到的是CSS盒子的思想 首先我们需要用一个大盒子作为主体页面 接着放三个小盒子 来完成页面的布局

2.2 相关代码的解释

</head>
<body>
<div class="box">    
    <div class="header">
        <ul>
            <li><a href="#" onclick="showContent('main')">首页</a></li>
            <li><a href="#" onclick="showContent('user')">用户管理</a></li>
            <li><a href="#" onclick="showContent('artical')">文章管理</a></li>
        </ul>
    </div>
    <div class="article">
      <h1 class="title">欢迎来到后台管理系统</h1>
    </div>
    <div class="content" id="content">
    <p class="title2">工作区域</p>
    </div>

第一个盒子是header 也就是导航栏部分 这里有三个用链接a标签做成的按钮 用以实现页面的跳转 第二个盒子是artical 也就是最上面的部分 第三个盒子是content,也就是主要内容的呈现 当用户点击时 实现页面的跳转

 然后我们再来看看css的相关代码

 <style type="text/css">
        /* 用以消除其他标签自带的margin和padding 防止盒子被撑大 */
        *{
            margin: 0;
            padding: 0;
        }
        /* 主体盒子的相关设定
            1.margin 设置成居中的样式 0表示上下无间距 左右居中
            2.box-sizing 保证盒子的原有尺寸
        */
        .box{
            background-color: rgb(243,246,248);
            margin: 0 auto;
            width: 1080px;
            height: 1080px;
            box-sizing: border-box;
        }
        /* 将ul标签自带的序号点消除 */
        ul{
            list-style: none;
        }
        /* 将文字居中 */
        .header li{
            padding: 15px;
            text-align: center;
        }
        /* 消除下划线 */
        a{
            text-decoration: none;
            color:rgb(98,95,92)
        }
        /* 利用浮动的思想 将该盒子左浮动 位置固定在左边*/
        .header{
            float: left;
            background-color: rgb(243,246,248);
            width: 160px;
            height: 1080px;
            position: fixed;
        }
        .article{
            float: right;
            width: 910px;
            height: 71px;
            background-color: rgb(255,255,255);
            margin-left:8px ;
            border-left:8px solid rgb(0,116,114) ;
            box-sizing: border-box;

        }
        .title{
            line-height: 71px;
            margin-left: 8px;
        }
        /* 当光标停留在标签a上时 文字颜色变成红色 */
        a:hover{
            color: red;
        }
        .content{
            float: right;
            width: 910px;
            height: 1001px;
            background-color: rgb(255,255,255);
            margin-top: 8px;
        }
        .title2{
            text-align: center;
            margin: 8px;
        }
        .title3{
            margin: 8px;
        }
    </style>

3.源代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        /* 用以消除其他标签自带的margin和padding 防止盒子被撑大 */
        *{
            margin: 0;
            padding: 0;
        }
        /* 主体盒子的相关设定
            1.margin 设置成居中的样式 0表示上下无间距 左右居中
            2.box-sizing 保证盒子的原有尺寸
        */
        .box{
            background-color: rgb(243,246,248);
            margin: 0 auto;
            width: 1080px;
            height: 1080px;
            box-sizing: border-box;
        }
        /* 将ul标签自带的序号点消除 */
        ul{
            list-style: none;
        }
        /* 将文字居中 */
        .header li{
            padding: 15px;
            text-align: center;
        }
        /* 消除下划线 */
        a{
            text-decoration: none;
            color:rgb(98,95,92)
        }
        /* 利用浮动的思想 将该盒子左浮动 位置固定在左边*/
        .header{
            float: left;
            background-color: rgb(243,246,248);
            width: 160px;
            height: 1080px;
            position: fixed;
        }
        .article{
            float: right;
            width: 910px;
            height: 71px;
            background-color: rgb(255,255,255);
            margin-left:8px ;
            border-left:8px solid rgb(0,116,114) ;
            box-sizing: border-box;

        }
        .title{
            line-height: 71px;
            margin-left: 8px;
        }
        /* 当光标停留在标签a上时 文字颜色变成红色 */
        a:hover{
            color: red;
        }
        .content{
            float: right;
            width: 910px;
            height: 1001px;
            background-color: rgb(255,255,255);
            margin-top: 8px;
        }
        .title2{
            text-align: center;
            margin: 8px;
        }
        .title3{
            margin: 8px;
        }
    </style>
</head>
<body>
<div class="box">    
    <div class="header">
        <ul>
            <!-- onclick 绑定相应的事件 -->
            <li><a href="#" onclick="showContent('main')">首页</a></li>
            <li><a href="#" onclick="showContent('user')">用户管理</a></li>
            <li><a href="#" onclick="showContent('artical')">文章管理</a></li>
        </ul>
    </div>
    <div class="article">
      <h1 class="title">欢迎来到后台管理系统</h1>
    </div>
    <div class="content" id="content">
    <p class="title2">工作区域</p>
    </div>
    <!-- 利用脚本语言实现页面的跳转 -->
    <script>
        function showContent(id) {
          var content = document.getElementById('content');
          if (id === 'main') {
            content.innerHTML = '<h1 class="title3">Welcome to my website!</h1><p class="title3">This is the home page.</p>';
          } else if (id === 'user') {
            content.innerHTML = '<h1 class="title3">About Us</h1><p class="title3">Learn more about our company.</p>';
          } else if (id === 'artical') {
            content.innerHTML = '<h1 class="title3">Our Services</h1><p class="title3">Discover what we can do for you.</p>';
        }
    }
      </script>
</div>
</body>
</html>

关于相关页面的跳转 也可以用iframe标签 就不多赘述啦~  再见

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 CSS 设置导航栏跳转链接,你可以使用 `a:link` 选择器来指定未访问链接的样式,使用 `a:visited` 选择器来指定已访问链接的样式,使用 `a:hover` 选择器来指定鼠标悬停在链接上时的样式,以及使用 `a:active` 选择器来指定链接被点击时的样式。 例如,下面的 CSS 代码指定了未访问链接为蓝色,已访问链接为红色,鼠标悬停在链接上时为绿色,点击链接时为黄色: ```css a:link { color: blue; } a:visited { color: red; } a:hover { color: green; } a:active { color: yellow; } ``` 你还可以使用类似于 `.nav-link` 的类选择器来设置导航栏的链接样式,如下所示: ```css .nav-link { color: blue; } .nav-link:visited { color: red; } .nav-link:hover { color: green; } .nav-link:active { color: yellow; } ``` 在 HTML ,你可以使用 `<a>` 标签来创建链接,如下所示: ```html <a href="http://www.example.com" class="nav-link">Example</a> ``` ### 回答2: CSS是一种用于网页设计的样式表语言,通过它我们可以实现网页的布局、样式和交互效果。要设置导航栏的跳转链接,首先需要在HTML定义导航栏的结构,然后使用CSS为其添加样式和设置链接。 首先,我们可以使用HTML的<nav>标签定义导航栏的容器,并在其使用<ul>和<li>标签来创建导航项。每个导航项可以使用<a>标签定义链接,并在href属性设置跳转的目标链接,同时在<a>标签内添加链接文本。 例如,HTML的代码可以是这样的: ```HTML <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">产品</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> ``` 接下来,我们可以使用CSS来设置导航栏的样式和链接的效果。可以通过选择器选导航栏的<a>标签,并为其添加样式,例如设置文字颜色、字体大小、背景颜色等。可以通过:hover伪类选择器为链接添加鼠标悬停时的样式,例如设置背景颜色的改变、文字颜色的改变等,以增加用户体验。 例如,CSS的代码可以是这样的: ```CSS nav ul { list-style: none; padding: 0; margin: 0; } nav li { display: inline; margin-right: 10px; } nav a { text-decoration: none; color: #333; font-size: 16px; padding: 5px; background-color: #eee; } nav a:hover { background-color: #ccc; color: #fff; } ``` 通过以上HTMLCSS的设置,我们就可以实现导航栏的跳转链接了。当用户点击导航项时,页面就会跳转到所设置的链接地址。同时,通过CSS的样式设置,我们还可以为导航栏添加各种效果,以增强页面的可读性和美观性。 ### 回答3: 要设置导航栏的跳转链接,我们可以使用CSS来添加样式和实现效果。首先,我们需要为导航栏元素创建一个包含链接的HTML结构。例如,我们可以使用无序列表 `<ul>` 和列表项 `<li>` 来创建导航栏HTML代码示例: ```html <ul class="navigation"> <li><a href="#">主页</a></li> <li><a href="#">关于</a></li> <li><a href="#">服务</a></li> <li><a href="#">联系</a></li> </ul> ``` 然后,我们可以使用CSS选择器来选择导航栏链接并添加样式。你可以使用类选择器、ID选择器或者标签选择器来选择导航栏链接。下面是一个示例使用类选择器来选择导航链接的CSS代码: CSS代码示例: ```css .navigation li { display: inline-block; /* 让列表项水平排列 */ margin-right: 10px; /* 列表项之间的间距 */ } .navigation li a { text-decoration: none; /* 去除链接的下划线 */ color: #000; /* 设置链接的颜色 */ } .navigation li a:hover { color: #f00; /* 悬停时改变链接的颜色 */ } ``` 这里的 `.navigation` 是导航栏的类名选择器。`display: inline-block;` 使得列表项水平排列,`margin-right: 10px;` 设置列表项之间的间距。`.navigation li a` 选择所有导航栏链接,并设置其样式,`text-decoration: none;` 去除链接的下划线,`color: #000;` 设置链接的颜色为黑色。`.navigation li a:hover` 规定了鼠标悬停在链接上时的样式,这里将链接的颜色改为红色。 通过设置这些样式,我们实现导航栏的基本样式和链接的跳转效果。你可以根据需要自定义样式和链接的跳转目标来进一步完善导航栏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值