使用jquery库实现电梯导航效果

一般来说,一些大型的电商网站首页,页面内容很多,页面会很长,这时候大部分网站都选择使用电梯导航,使页面跳转到相应位置或者返回顶部。

下面使用jquery库来实现电梯导航

基本思路

电梯导航基本上就是使用元素距离页面头部的高度offsetTop和页面滚动的距离scrollTop来进行比较事项相应的效果。

1、页面滚动到相应的位置,实现电梯导航的显示与隐藏
2、页面滚动到相应的位置,电梯导航的按钮添加或者移出相应的类
3、点击电梯导航按钮,实现页面的滚动和为按钮添加或者移出相应的类
4、点击顶部按钮,返回顶部

代码实现

html代码

<div class="header">头部</div>
<div class="banner">焦点图</div>
    <div class="content">
        <div class="qinzi w">亲子</div>
        <div class="liren w">丽人</div>
        <div class="xuexi w">学习</div>
        <div class="lvyou w">旅游</div>
        <div class="zhusu w">住宿</div>
        <div class="meishi w">美食</div>
    </div>
    <div class="footer">尾部</div>

    <!-- 电梯导航 -->
    <div class="floor" style="display: none;">
        <ul>
            <li>亲子</li>
            <li>丽人</li>
            <li>学习</li>
            <li>旅游</li>
            <li>住宿</li>
            <li>美食</li>
        </ul>
        <span>顶部</span>
</div> 

css代码

 *{
            padding: 0;
            margin: 0;
        }
        body {
            font-size: 30px;
        }

        .header {
            width: 1100px;
            height: 200px;
            background-color: pink;
            margin: 0 auto;
        }

        .banner {
            width: 1100px;
            height: 400px;
            background-color: skyblue;
            margin: 0 auto;
        }

        .footer {
            width: 1100px;
            height: 300px;
            background-color: darkolivegreen;
            margin: 0 auto;
        }

        .content {
            width: 1100px;
            margin: 0 auto;
        }
        .content .qinzi {
            width: 100%;
            height: 324px;
            background-color: rosybrown;
        }

        .content .liren {
            width: 100%;
            height: 304px;
            background-color: slategrey;
        }

        .content .xuexi {
            width: 100%;
            height: 300px;
            background-color: khaki;
        }

        .content .lvyou {
            width: 100%;
            height: 300px;
            background-color: greenyellow;
        }

        .content .zhusu {
            width: 100%;
            height: 300px;
            background-color: darkcyan;
        }

        .content .meishi {
            width: 100%;
            height: 300px;
            background-color: lightgreen;
        }

        .floor {
            width: 50px;
            position: fixed;
            top: 150px;
            left: 50%;
            margin-left: -620px;
            font-size: 16px;
            text-align: center;
        }

        .floor li {
            width: 50px;
            height: 30px;
            background-color: grey;
            margin-bottom: 5px;
            line-height: 30px;
            list-style: none;
            cursor: pointer;
        }
        span {
            display: block;
            width: 50px;
            height: 30px;
            background-color: grey;
            margin-bottom: 5px;
            line-height: 30px;
            list-style: none;
            cursor: pointer;
        }
        .floor .current {
            background-color: hotpink;
        }

JavaScript代码

var flag = true;  //使用节流阀
        //页面刚开始隐藏,当页面滚动到content的时候,电梯导航显示
        $(function () {
            //页面刷新时调用一次
            //封装函数,切换显示与隐藏
            var contentTop = $(".content").offset().top;
            toggleTool();
            function toggleTool() {
                if ($(document).scrollTop() >= contentTop) {
                    $(".floor").fadeIn();

                } else {
                    $(".floor").fadeOut();
                }

            }
            $(window).scroll(function () {
                toggleTool()
                //页面滚动到相应位置,电梯导航按钮添加current类
                if (flag) {
                    $('.content .w').each(function (i, ele) {
                        var cuHeight = ele.offsetHeight / 2;
                        if ($(document).scrollTop() >= $(ele).offset().top - cuHeight) {
                            $('.floor li').eq(i).addClass('current').siblings().removeClass();

                        }

                    })
                }

            })

            //点击电梯导航按钮,页面跳转到相应位置,使用jquery里面的animate
            $('.floor li ').click(function () {
                flag = false;
                $(this).addClass('current').siblings().removeClass();
                var index = $(this).index();
                var current = $('.content .w').eq(index).offset().top;
                $('html,body').stop().animate({
                    scrollTop: current
                }, function () {
                    flag = true;
                })
            })
            $('.floor span').click(function () {
                $(this).addClass('current');
                $('html,body').stop().animate({
                    scrollTop: 0
                })
      })
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 基于jquery实现导航菜单高亮显示(两种方法)

    项目需求: 实现原理:当选中当前元素时,给当前元素添加样式,同级元素移除样式. 点击不同的导航菜单实现当前点击的菜单是高亮的,点击导航下面的某个分类,分类所属的导航也必须是高亮的,点击某一篇文章,文章所属的导航菜单也必须是高亮的. 效果图如下: 示例代码一: 具体示例代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/x

  • jQuery实现电梯导航模块

    本文实例为大家分享了jQuery实现电梯导航模块的具体代码,供大家参考,具体内容如下 功能模块 1.当页面滚动到电器的模块, 显示电梯导航 2.点击电梯导航中的 li, 页面滚动到对应的内容区域 3.为当前点击的 li 添加 current 类 4.页面内容滚动到第几个区域, 左侧的电梯导航自动高亮对应的 li 5.为页面滚动设置节流阀(点击 li 时, 不进行第4步判断) html部分结构 <ul class="sidebar"> <li>手机模块</l

  • jquery仿京东导航/仿淘宝商城左侧分类导航下拉菜单效果

    在网站建设中,特别是做商城和产品网站,通常会用到导航弹出菜单,像是jquery写的仿京东导航菜单,一个经典的左侧多级导航菜单,学会了可以任意改变布局.京东菜单已经有不少JS前端爱好者仿写过,今天蚂蚁网络重新与大家分享一款仿京东商城的商品多级分类菜单,精简版代码 先看下jquery仿京东导航效果: 前端html代码如下: 复制代码 代码如下: <ul> <li><a href="#">baidu</a></li> <div

  • 简单的jquery左侧导航栏和页面选中效果

    这里是要实现导航的左侧并选中的,此功能需引用jquery 左侧导航: <div class="box"> <ul class="menu"> <li class="level1"> <a href="#none" rel="external nofollow" rel="external nofollow" rel="external

  • JQuery 浮动导航栏实现代码

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&q

  • JQuery实现电梯导航特效

    本文分享一个基于JQuery实现的电梯导航效果,效果如下: 以下是代码实现: <!DOCTYPE html> <html lang="en">   <head>     <meta charset="utf-8" />     <title>基于JQuery实现电梯导航特效</title>     <style type="text/css">         *

  • jquery 面包屑导航 具体实现

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>YanCms

  • JQuery实现鼠标滑过显示导航下拉列表

    当网站导航栏项目很多的时候,我们往往是将同一级目录下的栏目先隐藏起来.当用户的鼠标滑过时则显示出来.这就是用javascript实现的一个导航栏下拉列表.小编一步步给大家讲解.值得注意的是我们使用的是Javascript的一个框架Jquery来实现的.所以,你在使用的时候必须要下载Jquery. 先建立HTML代码 复制代码 代码如下: <html> <head> <meta http-equiv="Content-Type" content="

  • jquery实现点击变换导航样式的方法

    本文实例讲述了jquery实现点击变换导航样式的方法.分享给大家供大家参考.具体如下: 这里分享一个导航点击变换样式的代码,近来看到很多网站的导航都是点击后导航样式跟着变化,今天就使用jqurey简单实现这一功能,高手勿喷. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-click-cha-nav-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.

  • jQuery 顶部导航跟随滚动条滚动固定浮动在顶部

      复制代码 代码如下: <section> <article class="left"> <p> </p> <ul> <li><a href="http://freejs.net/article_jquerywenzi_149.html" title="Ajax 动态加载内容">Ajax 动态加载内容</a></li> <li>

随机推荐