//闭包限定命名空间 (function ($) { $.fn.extend({ "mousemoveblock": function (options) { //左右拉动函数插件封装 //默认参数 var defaluts = { content:$(this).children("div"), //内容块 width: $(this).children("div").width() //内容块宽度 } //检测用户传进来的参数是否合法 if (!isvalid(options)) return this; var opts = $.extend({}, defaluts, options); //使用jquery.extend 覆盖插件默认参数 return this.each(function () { //这里的this 就是 jquery对象。这里return 为了支持链式调用 //遍历所有的要移动的块,当调用 mousemoveblock()插件的是一个集合的时候。 var $this = $(this); //获取当前dom的jquery对象,这里的this是当前循环的dom mouseoverblock($this, opts.content, opts.width); //调用每个要移动的块的方法 }); } }); //私有方法,检测参数是否合法 function isvalid(options) { return !options || (options && typeof options === "object") ? true : false; } //私有方法,块移动的函数 function mouseoverblock(showblock, contentblock, contentwidth) { //视窗块、内容块、内容宽 var wwidth = $(showblock).width(), //显示块宽 bwidth = contentwidth, //内容块宽 leftvalue = 0, //内容块向左拉动的值 windowwidth = $(window).width(); function winwidth() { var winwidth = 0; if (window.innerwidth) winwidth = window.innerwidth; else if ((document.body) && (document.body.clientwidth)) winwidth = document.body.clientwidth; if (document.documentelement && document.documentelement.clientwidth) winwidth = document.documentelement.clientwidth; return winwidth; } function winheight() { var winheight = 0; if (window.innerheight) winheight = window.innerheight; else if ((document.body) && (document.body.clientheight)) winheight = document.body.clientheight; if (document.documentelement && document.documentelement.clientheight) winheight = document.documentelement.clientheight; return winheight; } //返回鼠标位置 function getmousepos(event) { var e = event || window.event; var scrollx = document.documentelement.scrollleft || document.body.scrollleft; var x = e.pagex || e.clientx + scrollx; // x = x - (windowwidth - wwidth)/2; return { 'x': x }; } //鼠标左右拉动内容 function contentfunction(event) { /*鼠标位置除于视窗宽的百分比,乘于内容宽与视窗宽的差*/ leftvalue = (-(getmousepos(event).x)) / winwidth() * (600 - 200); $(contentblock).css({ left: leftvalue + 600 }); } //事件绑定 $(window).bind("mousemove", function (event) { contentfunction(event); }) $(window).resize(function () { wwidth = $(showblock).width(); leftvalue = 0; $(contentblock).css({ left: leftvalue }); }) } })($); //(function ($) { // $.fn.extend({ // "mouseoverblock": function (options) { // } // }) //})(jquery)