jQuery实现固定侧边栏

本文介绍如何利用jQuery实现侧边栏在页面滚动时保持固定位置的效果,从而提升用户体验。通过添加适当的CSS和JavaScript代码,可以确保侧边栏在内容滚动时始终保持可见。
摘要由CSDN通过智能技术生成

效果:
请添加图片描述

代码:

<style>
        .w {
     
            width: 1000px;
            margin: 0 auto;
            margin-top: 10px;
        }
        
        .header {
     
            height: 200px;
            background-color: purple;
        }
        
        .banner {
     
            height: 300px;
            background-color: skyblue;
        }
        
        .main {
     
            height: 1000px;
            background-color: yellowgreen;
        }
        
        span {
     
            display: none;
            position: absolute;
            bottom: 0;
        }
        
        .slider-bar {
     
            position: absolute;
            top: 400px;
            left: 50%;
            margin-left: 500px;
            width: 30px
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Bootstrap和jQuery实现导航侧边栏的HTML代码示例: ```html <!DOCTYPE html> <html> <head> <title>Navigation Sidebar Example</title> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <!-- jQuery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <!-- Bootstrap JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> /* Sidebar styling */ .sidebar { height: 100%; width: 0; position: fixed; z-index: 1; top: 0; left: 0; background-color: #111; overflow-x: hidden; transition: 0.5s; padding-top: 60px; } .sidebar a { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #818181; display: block; transition: 0.3s; } .sidebar a:hover { color: #f1f1f1; } .sidebar .closebtn { position: absolute; top: 0; right: 25px; font-size: 36px; margin-left: 50px; } .openbtn { font-size: 20px; cursor: pointer; background-color: #111; color: white; padding: 10px 15px; border: none; } .openbtn:hover { background-color: #444; } /* Page content styling */ #main { transition: margin-left .5s; padding: 20px; } /* Responsive media query for smaller screens */ @media screen and (max-height: 450px) { .sidebar {padding-top: 15px;} .sidebar a {font-size: 18px;} } </style> </head> <body> <!-- Navigation sidebar --> <div id="mySidebar" class="sidebar"> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a> <a href="#">Home</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Contact</a> </div> <!-- Page content --> <div id="main"> <button class="openbtn" onclick="openNav()">☰ Navigation</button> <h1>Welcome to my website!</h1> <p>This is an example of how to create a navigation sidebar using Bootstrap and jQuery.</p> </div> <script> // Open the sidebar function openNav() { document.getElementById("mySidebar").style.width = "250px"; document.getElementById("main").style.marginLeft = "250px"; } // Close the sidebar function closeNav() { document.getElementById("mySidebar").style.width = "0"; document.getElementById("main").style.marginLeft = "0"; } </script> </body> </html> ``` 这段代码实现了一个具有导航侧边栏的网页,点击“Navigation”按钮可以打开或关闭侧边栏。该示例使用了Bootstrap的CSS和JavaScript库以及jQuery库来实现导航侧边栏的样式和交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值