jQuery实现页面滚动时智能浮动定位

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jQuery实现页面滚动时层智能浮动定位_前端开发</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

body {
            font: 12px/1.8 Arial;
            color: #666;
        }

h1.tit-h1 {
            font-size: 38px;
            text-align: center;
            margin: 30px 0 15px;
            color: #f60;
        }

.wrap {
            border: 1px dashed #ccc;
            background: #f8f8f8;
            padding: 20px;
        }

.demo {
            height: 1500px;
        }

.float {
            width: 80px;
            padding: 10px;
            border: 1px solid #ffecb0;
            background-color: #fffee0;
            -moz-box-shadow: 1px 1px 2px rgba(0,0,0,.2);
            -webkit-box-shadow: 1px 1px 2px rgba(0,0,0,.2);
            box-shadow: 1px 1px 2px rgba(0,0,0,.2);
            position: absolute;
            right: 10%;
            top: 131px;
        }
    </style>
    <script type="text/javascript" src="../..content/js/jquery-1.4.4.min.js"></script>
</head>
<body>

<h1 class="tit-h1">jQuery实现页面滚动时层智能浮动定位_前端开发</h1>
    <div class="wrap">
        <div class="demo"></div>
        <div class="float" id="float">我在这里等你噢!</div>
    </div>
    <script type="text/javascript">
        $.fn.smartFloat = function () {
            var position = function (element) {
                var top = element.position().top, pos = element.css("position");
                $(window).scroll(function () {
                    var scrolls = $(this).scrollTop();
                    if (scrolls > top) {
                        if (window.XMLHttpRequest) {
                            element.css({
                                position: "fixed",
                                top: 0
                            });
                        } else {
                            element.css({
                                top: scrolls
                            });
                        }
                    } else {
                        element.css({
                            position: pos,
                            top: top
                        });
                    }
                });
            };
            return $(this).each(function () {
                position($(this));
            });
        };
        //绑定
        $("#float").smartFloat();
    </script>
    </div>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push([‘_setAccount‘, ‘UA-20541395-2‘]);
        _gaq.push([‘_trackPageview‘]);
        (function () {
            var ga = document.createElement(‘script‘); ga.type = ‘text/javascript‘; ga.async = true;
            ga.src = (‘https:‘ == document.location.protocol ? ‘https://ssl‘ : ‘http://www‘) + ‘.google-analytics.com/ga.js‘;
            var s = document.getElementsByTagName(‘script‘)[0];
            s.parentNode.insertBefore(ga, s);
        })();
    </script>
</body>
</html>

时间: 2024-08-10 23:29:45

jQuery实现页面滚动时智能浮动定位的相关文章

基于jQuery实现页面滚动时顶部导航显示隐藏效果

<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <link rel=&quo

js页面滚动时层智能浮动定位实现:

案例展示: 第一个:每个人都有看过的淘宝页面: 案例就不多说了,有兴趣的可以多去找找, 这样的智能浮动效果还是不错的,挺方便. 基本的实现原理: 首先有一个默认状态就是没有滚动的时候,什么事情也不用做,定位可以是absolute,也可以是stati,fixed,relartive 这些都可以.我们主要需要关注的是当浏览器滚动的时候,(要浮动的块)要移除浏览器界面视区的时候,修改其position属性,让其浮动在窗口的上沿显示就可以了.最好的position属性就是fixed了,可以在IE6+和其

Javascript实现页面滚动时导航智能定位

遇到的问题: 在做官网的时候,需要滚动定位的区块的图片不确定,无法确定用户浏览区域对应的模块导航 之前的解决方案是: 通过定位滚动条的位置来判断用户浏览区域对应的模块导航,这种方法的弊端是,区块的高度不确定时就无法计算滚动条的位置来判断: 优化方案: 页面中会有多个模块,每个模块对应一个导航,当页面滚动到某个模块时,对应的模块导航需要加上一个类用于区分当前用户所浏览区域. 以下是DEMO <!-- html --><div class="container">

Javascript页面滚动时导航智能定位

常见的开发页面中可能会有这么一个需求,页面中会有多个模块,每个模块对应一个导航,当页面滚动到某个模块时,对应的模块导航需要加上一个类用于区分当前用户所浏览区域. 假设结构如下: <div class="container"> <div class="wrapper"> <div class="section" id="section1">section1</div> <di

JQuery Mobile+JS实现智能浮动定位导航条

实现原理 其实很简单,主要用到几个知识点:JQuery Mobile+JS实现智能浮动定位导航条,布布扣,bubuko.com

jQuery判断页面滚动到底部

有时候我们需要页面滚动到底部的时候去加载更多的数据,实现思路基本上就是判断浏览器页面是否滚动到了页面底部,当滚动到页面底部的时候,用AJAX异步获取数据然后加载到页面中. 利用jQuery判断浏览器页面是否滑动到了底部: <script type="text/javascript"> // scroll event $(window).scroll(function(){ // scroll at bottom if ($(window).scrollTop() + $(w

js原生 + jQuery实现页面滚动字幕

js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 代码如下: <div class="box"> <ul class="list"> <li>这是滚动加载的第1条数据</li> <li>你猜猜这是第几条滚动加载的文字</li> <li>

页面滚动时出现固定导航

页面滚动时出现固定导航的效果,参考页面有http://v.thea.cn/course/info_358和v.thea.cn. 参考代码如下: <div class="tab-title" style="position: static; top: 0px;"><span class="normal selected">课程介绍</span><span class="normal"&g

jquery “做页面滚动到某屏时改变状态标题” 所用知识点记录

浏览器滚动条滚动时触发事件 //浏览器滚动条滚动时触发事件 $(window).scroll(function(){}); 浏览器窗口大小改变时触发事件 //浏览器窗口大小改变时触发事件 $(window).resize(function(){}); 监听所有锚点链接实现平滑移动 //监听所有锚点链接实现平滑移动 $('a[href*=#],area[href*=#]').click(function() { if (location.pathname.replace(/^\//, '') ==