如何使用CSS3+JQuery实现悬浮墙式菜单

yizhihongxing

首先,让我们来了解什么是悬浮墙式菜单。悬浮墙式菜单一般出现在页面一侧或角落的固定位置,当页面滚动时,菜单会跟随滚动而移动,保持在视野范围内,以方便用户随时能够使用。

为了实现这个效果,我们可以使用CSS3中的position属性和Jquery中的scroll()方法。下面是具体的实现步骤:

  1. CSS部分:

首先,在HTML页面中,创建一个DIV元素,这个DIV元素就是悬浮的菜单。然后,为这个DIV元素添加样式,使其具有固定的位置,并且宽高、背景等样式可以自行设置。CSS代码如下所示:

#menu {
    position: fixed;
    top: 50px;  // 距离顶部50px
    left: 0;
    width: 200px;
    height: 300px;
    background: #fff;
    border: 1px solid #ccc;
    z-index: 999;  // 显示优先级最高
}
  1. JQuery部分:

在页面加载时,通过JQuery获取到menu元素,并获取到页面中与menu元素距离顶部的距离(offset.top)。然后,在滚动页面时,判断滚动距离和menu元素到顶部的距离的大小关系,动态设置menu元素的top值,使其保持固定位置。代码如下所示:

$(document).ready(function() {
    var menuOffset = $("#menu").offset().top;  // 距离顶部的距离
    $(window).scroll(function() {
        if ($(this).scrollTop() > menuOffset ) {
            $("#menu").css("top",$(this).scrollTop()-menuOffset);  // 设置top值,使其保持固定位置
        } else {
            $("#menu").css("top","0");  // 还原top值
        }   
    });
});

至此,一个简单的悬浮墙式菜单就完成了。下面,我们来看一下具体的应用案例。

示例一:带有浮动效果的悬浮菜单

这个实例是在之前的基础上进行了扩展,让菜单呈现出类似海浪浮动的效果。我们可以使用JQuery的animate()方法,让菜单在上下滑动时,带有缓动效果,使其看起来更加生动。

HTML代码:

<div id="menu">
    <ul>
        <li><a href="#section1">Section 1</a></li>
        <li><a href="#section2">Section 2</a></li>
        <li><a href="#section3">Section 3</a></li>
        <li><a href="#section4">Section 4</a></li>
        <li><a href="#section5">Section 5</a></li>
    </ul>
</div>
<div id="section1">
    <h2>Section 1</h2>
    <p>Content Section 1</p>
</div>
<div id="section2">
    <h2>Section 2</h2>
    <p>Content Section 2</p>
</div>
<div id="section3">
    <h2>Section 3</h2>
    <p>Content Section 3</p>
</div>
<div id="section4">
    <h2>Section 4</h2>
    <p>Content Section 4</p>
</div>
<div id="section5">
    <h2>Section 5</h2>
    <p>Content Section 5</p>
</div>

CSS代码:

#menu {
    position: fixed;
    top: 50px;
    left: 0;
    width: 200px;
    height: 300px;
    background: #fff;
    border: 1px solid #ccc;
    z-index: 999;
    overflow: hidden;  // 隐藏菜单内容的滚动条
}

#menu ul {
    padding: 0;
    margin: 0;
    list-style: none;
}

#menu li {
    padding: 10px;
    border-bottom: 1px solid #ccc;
}

#menu li a {
    text-decoration: none;
}

#section1,#section2,#section3,#section4,#section5 {
    margin-top: 400px;
    height: 500px;
}

#section1 { background: #FDEDEC; }
#section2 { background: #EAF2F8; }
#section3 { background: #FEF9E7; }
#section4 { background: #E8F8F5; }
#section5 { background: #FAE5D3; }

JQuery代码:

$(document).ready(function() {
    var menuOffset = $("#menu").offset().top;
    $(window).scroll(function() {
        if ($(this).scrollTop() > menuOffset ) {
            $("#menu").animate({top:$(this).scrollTop()-menuOffset}, 200, 'swing');  // 使用animate()方法,添加缓动效果
        } else {
            $("#menu").animate({top:0}, 200, 'swing');
        }   
    });
});

示例二:带有搜索框的悬浮菜单

这个实例在之前的基础上加入了搜索框,让菜单功能更加完善。我们可以先将悬浮菜单绝对定位,然后在菜单中添加搜索框元素,并使用CSS设置其样式。在滚动页面时,可以动态改变菜单的top和left值,使搜索框变成固定位置。代码如下所示:

HTML代码:

<div id="menu">
    <ul>
        <li><a href="#section1">Section 1</a></li>
        <li><a href="#section2">Section 2</a></li>
        <li><a href="#section3">Section 3</a></li>
        <li><a href="#section4">Section 4</a></li>
        <li><a href="#section5">Section 5</a></li>
    </ul>
    <div id="search-box">
        <input type="text" placeholder="Search...">
        <button type="submit">Search</button>
    </div>
</div>
<div id="section1">
    <h2>Section 1</h2>
    <p>Content Section 1</p>
</div>
<div id="section2">
    <h2>Section 2</h2>
    <p>Content Section 2</p>
</div>
<div id="section3">
    <h2>Section 3</h2>
    <p>Content Section 3</p>
</div>
<div id="section4">
    <h2>Section 4</h2>
    <p>Content Section 4</p>
</div>
<div id="section5">
    <h2>Section 5</h2>
    <p>Content Section 5</p>
</div>

CSS代码:

#menu {
    position: absolute;  // 使用绝对定位
    top: 100px;
    left: 0;
    width: 200px;
    background: #fff;
    border: 1px solid #ccc;
    z-index: 999;
    overflow: hidden;
}

#menu ul {
    padding: 0;
    margin: 0;
    list-style: none;
}

#menu li {
    padding: 10px;
    border-bottom: 1px solid #ccc;
}

#menu li a {
    text-decoration: none;
}

#search-box {
    padding: 10px;
}

#search-box input {
    width: 100%;
    padding: 5px;
    border: 1px solid #ccc;
    border-radius: 5px;
    outline: none;
}

#search-box button {
    margin-top: 10px;
    width: 100%;
    padding: 5px 10px;
    background-color: #ccc;
    border: none;
    border-radius: 5px;
    color: #fff;
    cursor: pointer;
}

#section1,#section2,#section3,#section4,#section5 {
    margin-top: 400px;
    height: 500px;
}

#section1 { background: #FDEDEC; }
#section2 { background: #EAF2F8; }
#section3 { background: #FEF9E7; }
#section4 { background: #E8F8F5; }
#section5 { background: #FAE5D3; }

JQuery代码:

$(document).ready(function() {
    var menuOffset = $("#menu").offset().top;
    var searchBoxOffset = $("#search-box").offset().top - menuOffset;  // 计算距离顶部的距离
    $(window).scroll(function() {
        if ($(this).scrollTop() > menuOffset ) {
            $("#menu").css({"top":$(this).scrollTop()-menuOffset, "left":$(this).scrollLeft()+10});  // 修改top和left值
            $("#search-box").css({"position":"fixed", "top":searchBoxOffset, "left":"10px"});  // 修改搜索框的top和left值
        } else {
            $("#menu").css({"top":"0px", "left":"0px"});
            $("#search-box").css({"position":"static"});
        }   
    });
});

通过以上两个示例,我们可以看到如何使用CSS3+JQuery来实现悬浮墙式菜单,并进行一些扩展,让菜单更加丰富和实用。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何使用CSS3+JQuery实现悬浮墙式菜单 - Python技术站

(0)
上一篇 2023年6月9日
下一篇 2023年6月9日

相关文章

  • CSS中的选择器种类总结及效率比较

    接下来我将详细讲解“CSS中的选择器种类总结及效率比较”的完整攻略。 CSS中的选择器种类总结及效率比较 CSS选择器是用于定位HTML文档中的元素并应用样式的工具。根据选择器的种类及其使用情况,CSS选择器可以分为多种类型。以下是CSS中选择器类型的总结及效率比较。 基本选择器 基本选择器是选择HTML标签、ID、class的选择器。这种选择器是最常用的选…

    css 2023年6月10日
    00
  • 详解纯css实现瀑布流(multi-column多列及flex布局)

    在这篇文章中,我将详细介绍如何使用纯CSS实现瀑布流布局。本文将涵盖以下两种方法:multi-column多列和flex布局。接下来,我将逐一介绍每种方法的实现步骤,并附上示例说明。 multi-column多列实现瀑布流布局 multi-column(多列)是CSS3的一个新属性,它可以将元素分成多列。通过将此属性应用于一个容器,并合理地设置一些其他属性,…

    css 2023年6月11日
    00
  • jQuery实现的图片轮播效果完整示例

    针对“jQuery实现的图片轮播效果完整示例”的攻略,我会做如下详细讲解。 1. 概述 图片轮播效果是常见的前端展示效果之一,借助jQuery可以快速、便捷地实现。以下是实现图片轮播效果的大致步骤: 编写HTML元素; 借助CSS样式美化HTML元素; 使用jQuery绑定事件及实现动画效果 2. HTML元素 在HTML中,首先需要准备一个div容器用于包…

    css 2023年6月10日
    00
  • css sprite原理优缺点及使用示例介绍

    CSS Sprite是一种web设计中比较流行的前端优化技术,它能从技术层面上减少网页的http请求次数,从而有效减轻服务器的负担,提高页面的加载速度和性能。下面来详细讲解CSS Sprite的原理、优缺点以及使用示例。 什么是CSS Sprite CSS Sprite是指将一个页面需要用到的小图标或者其他小图片合并成一张大图,再通过CSS的backgrou…

    css 2023年6月10日
    00
  • JointJS JavaScript流程图绘制框架解析

    JointJS JavaScript流程图绘制框架解析 JointJS是一款使用Javascript编写的流程图绘制框架,它可以帮助开发者快速构建出优美的流程图。下面我们将从以下几个方面对JointJS进行详细的介绍。 安装与快速上手 安装JointJS非常简单,只需使用npm进行安装即可。可以使用以下命令进行安装: npm install jointjs …

    css 2023年6月9日
    00
  • 使用CSS的clip-path属性实现不规则图形的显示

    使用CSS的clip-path属性可以实现不规则图形的显示。它能够将元素裁剪成各种形状,让你可以创造出独特的图形效果。下面是具体的攻略: 1. 了解clip-path属性 clip-path属性是用来设置元素裁剪路径的。它可以取值为none、url()、inset()、circle()、ellipse()、polygon()等,其中最常用的是polygon(…

    css 2023年6月10日
    00
  • CSS控制Table内外边框、颜色、大小示例

    下面我将详细讲解如何使用CSS控制HTML表格(Table)的内外边框、颜色和大小。 CSS控制表格外边框 表格外边框由表格的边框和表格外边距组成。CSS提供了多个属性,可以控制表格的外边框。以下是常用的属性及其用法: border属性 border属性用于设置表格的边框样式、宽度、颜色。 table { border: 1px solid black; }…

    css 2023年6月9日
    00
  • c# 爬取优酷电影信息(2)

    让我来为您详细讲解 “c# 爬取优酷电影信息(2)” 的完整攻略。 攻略概述: 本攻略将介绍如何使用 c# 爬取优酷电影信息。我们将使用 HttpClient 来发送 GET 请求,获取电影页面的 HTML 内容。然后,使用 HtmlAgilityPack 解析 HTML 内容,从而提取电影信息。最后,我们将使用 Console.WriteLine() 函数…

    css 2023年6月10日
    00
合作推广
合作推广
分享本页
返回顶部