zoukankan      html  css  js  c++  java
  • jquery插件:点击拉出的右侧滑动菜单

    就是一个停留在页面右侧的滑动菜单,点击可以拉出,带回调函数。宽高位置可以参数指定。插件代码如下:

    (jquery的路径请自己修改)

    (function($){
    $.fn.sideSwitch = function(opts){
        
        var defaults = {
            contentWidth:'400px',
            contentHeight:'185px',
            btnWidth:'30px',
            btnHeight:'80px',
            initTop:'',//初始化离浏览器顶部的距离
            extra:'',//因页面布局需要额外向左移动的距离
            callback:''//菜单拉出后的回调函数
            }
        var option = $.extend(defaults,opts);
        this.each(function(){//这一行的this是指$选择器选择到的对象,是一个数组
            var _this = $(this);//拿到某一个div
            var btndiv = _this.find('.ss_btn');
            var btnTop = (parseInt(option.contentHeight)-parseInt(option.btnHeight))/2;
            btndiv.css({option.btnWidth,height:option.btnHeight,top:btnTop,position:"absolute",cursor:"pointer"});
            var contentdiv = _this.find('.ss_content');
            var contentLeft = parseInt(option.btnWidth)+parseInt(btndiv.css('borderLeftWidth'))+parseInt(btndiv.css('borderRightWidth'));
            contentdiv.css({option.contentWidth,height:option.contentHeight,position:"relative",left:contentLeft,top:"0px"});
            
            var boxInitLeft = parseInt(document.body.clientWidth)-parseInt(option.btnWidth)-parseInt(btndiv.css('borderLeftWidth'))-parseInt(btndiv.css('borderRightWidth'))-option.extra;
            var boxInitWidth = parseInt(option.btnWidth)+parseInt(btndiv.css('borderLeftWidth'))+parseInt(btndiv.css('borderRightWidth'));
            _this.css({boxInitWidth,overflow:"hidden",position:"absolute",zIndex:999});
            if(!option.initTop){option.initTop = (parseInt(document.body.clientHeight)-parseInt(_this.css('height'))-parseInt(_this.css("borderTopWidth"))-parseInt(contentdiv.css("borderTopWidth")))/2;}
            _this.css({left:boxInitLeft,top:option.initTop});
            
            var menuYloc = _this.offset().top; //当前窗口的相对偏移
            $(window).scroll(function (){ 
            var offsetTop = menuYloc + $(window).scrollTop() +"px"; 
            _this.animate({top : offsetTop },{ duration:600 , queue:false }); 
            }); 
            
            btndiv.click(
              function(){
                  if(parseInt(_this.css("width"))<parseInt($('.ss_content').css('width'))){
                      var boxNewWidth = parseInt($('.ss_content').css('width'))+parseInt($('.ss_content').css('borderLeftWidth'))+parseInt($('.ss_content').css('borderRightWidth'))+parseInt($('.ss_btn').css('width'))+parseInt($('.ss_btn').css('borderLeftWidth'))+parseInt($('.ss_btn').css('borderRightWidth'));
                      
                      var boxNewLeft = parseInt(document.body.clientWidth)-boxNewWidth-option.extra;
                      _this.animate({left:boxNewLeft+'px',boxNewWidth+"px"},"slow");
                      option.callback();
                      }
                  else{
                      _this.animate({left:boxInitLeft,"30px"},"slow");        
                      }
    
    }
    )
    }); 
            
        
        }    
        
    })(jQuery)

    下面再附一个使用的demo:

    <!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=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
    *{margin:0;padding:0;}
    .div{float:left;width:30%;height:300px;border:1px solid #f00;}
    </style>
    <script type="text/javascript" src="../../jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="jquery.sideSwitch.js"></script>
    <script type="text/javascript">
    $(function(){
        $('#box').sideSwitch();
        });
    </script>
    </head>
    
    <body>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div class="div">文字文字文字</div>
    <div id="box"><!--侧边栏层--> 
        <div class="ss_btn">
            点我
        </div>
        <div class="ss_content"> 
        
        内容区域            
         
        </div> 
        
    </div> 
    <div id="mainbody"><!--主页内容--> 
    <p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p><p>主页的内容<p>
    </div> 
    </body>
    </html>
  • 相关阅读:
    java基础
    JAVASE 安装步骤
    MySQL 45道练习题
    MySQL 多表查询
    2018-08-07JDBC连接MySQL+增删改表格+SQL注入问题及其预处理对象PreparedStatement解决方案
    2018-08-06Java中的异常捕获和Throw详细细节
    2018-08-03List接口方法+LinkedList方法+Vector集合+Set接口下HashSet和LinkedHashSet集合+HashCode()+equals()方法对于Set接口判断重复的详细细节
    2018-08-01集合Collection+Iterator迭代器+泛型+增强For循环
    2018-07-31包装类与基本数据类型String的转换+System类详细知识+Arrays类+大数据(BigInteger+BigDecimal)运算
    2018-07-27Final+Static+匿名对象+3中代码块
  • 原文地址:https://www.cnblogs.com/zywf/p/4890898.html
Copyright © 2011-2022 走看看