jquery实现页面内链接锚点跳转平滑滚动效果

  • jQuery下实现锚点链接的平滑滚动(带浮动侧边栏)
    css+html+js
<style>
    * {
        margin: 0;
        padding: 0;
    }  
    body {
        font-size: 12px;
        line-height: 1.7;
    } 
    li {
        list-style: none;
    }
    #content {
        width: 800px;
        margin: 0 auto;
        padding: 20px;
    }
    #content h1 {
        color: #0088bb;
    }
    #content .item {
        padding: 20px;
        margin-bottom: 20px;
        border: 1px dotted #0088bb;
    }
    #content .item h2 {
        font-size: 16px;
        font-weight: bold;
        border-bottom: 2px solid #0088bb;
        margin-bottom: 10px;
    }  
    #content .item li {
        display: inline;
        margin-right: 10px;
    }
    #content .item li a img {
        width: 230px;
        height: 230px;
        border: none;
    }    
    #menu {
        position: fixed;
        top: 120px;
        right: 180px;
    }
    #menu ul li a {
        display: block;
        margin: 5px 0;
        font-size: 14px;
        font-weight: bold;
        color: #333;
        width: 80px;
        height: 50px;
        line-height: 50px;
        text-decoration: none;
        text-align: center;
    }
    #menu ul li a:hover,
    #menu ul li a.current {
        color: #fff;
        background: #0088bb;
    }
    /*ie6 hack*/
    * html,
    * html body {
        background-image: url(about:blank);
        background-attachment: fixed;
    } 
    * html #menu {
        /*position: fixed;*/
        position: absolute;
        top: expression(((e=document.documentElement.scrollTop)?e: document.body.scrollTop)+100+'px');
    }
    </style>
 <script>
    $(document).ready(function() {
        $(window).on('scroll', function() {
            var scrollTop = $(document).scrollTop();
            var menu = $("#menu");
            var items = $("#content").find(".item");
            var currentId = "";

            items.each(
                function() {
                    var im = $(this);
                    var itemTop = im.offset().top;
                    //console.log(itemTop);
                    if (scrollTop > (itemTop - 188)) {
                        currentId = "#" + im.attr("id");
                    } else {
                        return false;
                    }
                });

            var currentLink = menu.find(".current");
            if (currentId && currentLink.attr("href") != currentId) {
                currentLink.removeClass("current");
                menu.find("a[href=" + currentId + "]").addClass("current");
            }
        })
        var $a = $("a[href*='item']");
        /*            $a[1].onclick=function(){
                      console.log('123');
                    }*/
        $a.each(
            function() {
                console.log(this);
                $(this).on('click',
                    function() {
                        $('html,body').animate({
                            scrollTop: $($.attr(this, 'href')).offset().top
                        }, 500);
                        return false;
                    })
            }
        )

    });
    </script>
 <div id="menu">
        <ul>
            <!--运用锚点,实现导航定位。-->
            <li><a href="#item1" class="current">1F 男装</a></li>
            <li><a href="#item2">2F 女装</a></li>
            <li><a href="#item3">3F 美妆</a></li>
            <li><a href="#item4">4F 数码</a></li>
            <li><a href="#item5">5F 母婴</a></li>
        </ul>
    </div>
    <div id="content">
        <h1>地狗购物网</h1>
        <div id="item1" class="item">
            <h2>1F 男装</h2>
            <ul>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-b5b45b29072554f2.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
            </ul>
        </div>
        <div id="item2" class="item">
            <h2>2F 女装</h2>
            <ul>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c6860eb3ccb91287.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
            </ul>
        </div>
        <div id="item3" class="item">
            <h2>3F 美妆</h2>
            <ul>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-ac1bab21c95b5e97.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
            </ul>
        </div>
        <div id="item4" class="item">
            <h2>4F 数码</h2>
            <ul>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-3bb9aebf54bb385d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
            </ul>
        </div>
        <div id="item5" class="item">
            <h2>5F 母婴</h2>
            <ul>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
                <li>
                    <a href="#">![](http://upload-images.jianshu.io/upload_images/6348994-c2a985adf3ff41db.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a>
                </li>
            </ul>
        </div>
    </div>
  • JQuery实现简单的平滑过渡效果
<script type="text/JavaScript">  
$(document).ready(function() {  
    var $root = $('html, body');  //选择器缓存起来。这样每次点击时就不需要再重新查找了
    $('a').click(function() {
        var href = $.attr(this, 'href');
        $root.animate({
            scrollTop: $(href).offset().top
        }, 500, function () {
            window.location.hash = href;
        });
        return false;
    });
});  
</script>  
  • JQuery实现平滑过渡效果

改进版 好处是点击锚点链接平滑滚动到锚点,并且浏览器URL后缀不带有锚点字样

<script type="text/JavaScript">  
    $(document).ready(function() {  
      $('a[href*=#],area[href*=#]').click(function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
          var $target = $(this.hash);
          $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
          if ($target.length) {
            var targetOffset = $target.offset().top;
            $('html,body').animate({
              scrollTop: targetOffset
            },
            1000);
            return false;
          }
        }
    });
});  
</script> 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • <!DOCTYPE html> Document /* 标签样式初始化 */body{margin:0;p...
    三叶松阅读 763评论 0 2
  • 【综合】【学校教育】{办公文档}//www.greatytc.com/p/82aba897d1eb{办公文...
    5a15944f1ba2阅读 584评论 0 1
  • # 信息系统前沿课--范老师授课 #第二次作业## 一、重新使用Markdown ##熟悉语法,了解Markdow...
    thought_10c7阅读 678评论 0 0
  • 从小我就不是一个高情商的人,总觉得只要一片善心对人,别人也会认同我。殊不知世界上有一种叫做“就是看你不顺眼”,还有...
    莫吟雪阅读 374评论 0 3
  • 杂想一: 爱说话和不爱说话 爱说话和不爱说话 就像 对口相声里的逗哏和捧哏 一唱一和 杂想二: 大城市小城市 大城...
    卿莫迟阅读 170评论 0 0