jquery插件:点击拉出的右侧滑动菜单

摘要:
它是页面右侧的滑动菜单。单击它可使用回调函数将其拉出。宽度和高度位置可以由参数指定。插件代码如下:(function($){$.fn.sideSwitch=function(options){vardefaults={contentWidth:“400px”,contentHeight:“185px”,btnWidth:“30px”,btnHeight:“80px”,initTop:“”,//与浏览器顶部的初始化距离是额外的:“”,//页面布局需要与左侧回调的额外距离:“”//拉出菜单后的回调函数}varoption=$。扩展(默认值,选项);这每一个(function()){//这一行中的对象是指$选择器选择的对象,它是一个数组var_This=$(This);//获取一个divvarbtndiv=_This.find('.ss_btn');varbtnTop=(parseInt(option.contentHeight)-parseInt(options.btnEight))/2;btndiv.css({option.btnWidth,height:option.btnEights,top:btnTop,position:“absolute”,cursor:“pointer”});varcontentdiv=_ this.find('.ss_content');varcontentLeft=parseInt(option.btnWidth)+parseInt(btndiv.css('borderLeftWidth'))+parse Int(btndiv.css('border RightWidth');contentdiv.css({option.contentWidth,height:option.content-height,position:“relative”,left:contentLeft,top:“0px”});varboxInitLeft=parseInt(document.body.clientWidth)-parseInt(option.btnWidth)/parseInt。额外的varboxInitWidth=parseInt(option.btnWidth)+parseInt(btndiv.css('borderLeftWidth'))+parse Int(btndiv.css('border RightWidth');_这css({boxInitWidth,溢出:“hidden”,位置:“absolute”,zIndex:999});如果(!

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

(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>

免责声明:文章转载自《jquery插件:点击拉出的右侧滑动菜单》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python中的轻量级定时任务调度库:schedule基于无锁的C#并发队列实现下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

jQuery基础入门+购物车案例详解

jQuery是一个快速、简洁的JavaScript代码库(或JavaScript框架)。jQuery设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。 一、jQuery对象 1....

echarts-环形图处理图列中的点击,使百分比的数据列不发生变化,默认追加其他选项

将下列代码copy的echarts编辑器中 app.title = '环形图'; var $legendData = ['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']; var $legendSelected = {'直接访问':false}; var $seriesData = [ {value:335, name...

【jquery练习】横向滚动导航菜单

<!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"><...

jQuery根据ID、CLASS、等获取对象

Jquery是继prototype之后又一个优秀的Javascrīpt框架。它是轻量级的js库(压缩后只有21k) , 它兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。 jQuery使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJ...

第一百八十节,jQuery-UI,知问前端--消息提示 UI

jQuery-UI,知问前端--消息提示 UI 学习要点:   1.HTML 部分   2.CSS 部分   3.jQuery 部分 通过前面已学的 jQuery UI 部件,我们来创建一个注册表单。 html <div id="reg" title="会员注册"> <p> <label fo...

jQuery(1)

ajax 中get和post的区别1、get请求的数据大小限制小。追家在url中传送,能在浏览器中缓冲,能被看见,不安全。post以http消息的实体内容发送到web服务器,传送的数据量大。安全。 闭包:javascript语法区域中特殊区域。类似的方法闭包如 (function(){})();能访问局部变量。一个函数可以访问一个函数作用域中的变量。 j...