zoukankan      html  css  js  c++  java
  • jQuery实现页面滚动时层智能浮动定位

    各位兄弟可能碰到定位的问题,特别是在博客或者微博上面也会见到这个效果,于是产品人员在策划的时候就会要人家那种效果,,,而苦逼的我们需要去实现,实现实现。。。。。没办法,谁让我们是攻城师呢,,攻吧:

    效果图如下,滚动条下拉的时候,黑色的块TOP为0;固定显示:



    代码如下:

    <!DOCTYPE html>
    <html >
    <head>
    <title>jQuery实现页面滚动时层智能浮动定位</title>
    
    <meta name="description" content="" />
    <script type="text/javascript" src="http://jt.875.cn/js/jquery.js"></script>
    <style type="text/css"> 
    *{ margin:0; padding:0;}
    .top{height:100px; background:#ccc;text-align:center; line-height:100px; font-size:40px;}
    body { font:12px/1.8 Arial; color:#666; height:2000px;}
    .float{200px; height:200px; border:1px solid #ffecb0; background-color:#000;position:absolute; right:10px; top:150px;}
    </style> 
    </head>
    <body>
    <div class="top">导航啊导航啊导航</div>
    <div class="float" id="float"></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>
    </body> 
    </html>


  • 相关阅读:
    Response.Redirect 打开新窗体的两种方法
    linux下coredump的产生及调试方法
    AlertDialog具体解释
    数据仓库与数据挖掘的一些基本概念
    JS中setTimeout()的使用方法具体解释
    iOS开发- 查询项目代码行数
    STM32学习之路-LCD(3)&lt;显示图片&gt;
    谷歌技术&quot;三宝&quot;之MapReduce
    [ffmpeg 扩展第三方库编译系列] 关于libvpx mingw32编译问题
    javascript笔记
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2989524.html
Copyright © 2011-2022 走看看