zoukankan      html  css  js  c++  java
  • 固定浮动侧边栏(SmartFloat)

    <!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="Content-Type" content="text/html; charset=gb2312" />
    <title>固定浮动侧边栏</title>
    <style type="text/css">
    	body {
    		margin: 10px auto;
    		font-family: sans-serif;
    		 500px;
    	}
    	
    	div {
    		border-radius: 5px;
    		box-shadow: 1px 2px 5px rgba(0,0,0,0.3);
    		border: 1px solid #ccc;
    		padding: 5px;
    	}
    	
    	#sidebarWrap {
    		height: 400px;
    		 210px;
    		float: right;
    		position: relative;
    		box-shadow: none;
    		border: none;
    		margin: 0;
    		padding: 0;
    	}
    	
    	#main {
    		 270px;
    		height: 4000px;
    	}
    	
    	#footer {
    		clear: both;
    		margin: 10px 0;
    	}
    	
    	#sidebar {
    		 200px;
    		height: 300px;
    		position: absolute;
    	}
    	
    	#header {
    		height: 200px;
    		margin-bottom: 10px;
    	}
    	
    	#sidebar.fixed {
    		position: fixed;
    		top: 0;
    	}
    	
    	#footer { height: 600px; }
    </style>
    </head>
    
    <body>
        <div id="header">header</div>
        
        <div id="sidebarWrap">
            <div id="sidebar">Sidebar</div>
        </div>
        
        <div id="main">
            Main
        </div>
        
        <div id="footer">
            footer
        </div>
    </body>
    </html>
    <script src="../Js/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
        var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0));
        var footTop = $('#footer').offset().top - parseFloat($('#footer').css('marginTop').replace(/auto/, 0));
    
        var maxY = footTop - $('#sidebar').outerHeight();
    
        $(window).scroll(function(evt) {
            var y = $(this).scrollTop();
            if (y > top) {
                if (y < maxY) {
                    $('#sidebar').addClass('fixed').removeAttr('style');
                } else {
                    $('#sidebar').removeClass('fixed').css({
                        position: 'absolute',
                        top: (maxY - top) + 'px'
                    });
                }
            } else {
                $('#sidebar').removeClass('fixed');
            }
        });
    });
    </script>

  • 相关阅读:
    [985] 令牌放置
    [191] 位1的个数
    [637] 二叉树的层平均值
    大数据量时 Mysql LIMIT如何正确对其进行优化(转载)
    数据库中,表一sum得出一个值,赋给表二的某个字段,为null
    安装JDK提示: 该项不适于在指定状态下使用的错误
    hibernate中多对一问题
    JSONObject基本内容(三)
    JSONObject基本内容(二)
    JSONObject基本内容(一)
  • 原文地址:https://www.cnblogs.com/apollokk/p/6713878.html
Copyright © 2011-2022 走看看