jQuery实战-窗口效果
jQuery实战-窗口效果:
window.html
- <html>
- <head>
- <title>窗口效果</title>
- <meta http-equiv=”Content-Type” content=”text/html;charset=UTF-8”>
- <link type=”text/css” rel=”stylesheet” href=”css/window.css” />
- <script type=”text/javascript” src=”js/jquery.js”></script>
- <script type=”text/javascript” src=”js/ window.js”></script>
- </head>
- <body>
- <input type="button" value="左下角显示窗口" id="leftpop" />
- <input type="button" value="屏幕中间显示窗口" id="centerpop" />
- <div class="window">
- <div class="title" id="center">
- <img alt="关闭" src="images/close.gif" />
- 我是中间显示窗口的标题栏
- </div>
- <div class="content">
- 我是中间显示窗口的内容区
- </div>
- </div>
- <div class="window" id="left">
- <div class="title">
- <img alt="关闭" src="image/close.gif" />
- 我是左边显示窗口的标题栏
- </div>
- <div class="content">
- 我是左边显示窗口的内容区
- </div>
- </div>
- <div class="window" id="right">
- <div class="title">
- <img alt="关闭" src="image/close.gif" />
- 我是右边显示窗口的标题栏
- </div>
- <div class="content">
- 我是右边显示窗口的内容区
- </div>
- </div>
- </body>
- </html>
window.css
- .window {
- background-color: #D0DEF0;
- width: 250px;
- /*padding: 2px;*/
- margin: 5px;
- position: absolute; /*控制窗口绝对定位*/
- display:none;
- }
- .content {
- height: 150px;
- background-color: white;
- border: 2px solid #D0DEF0;
- padding: 2px;
- overflow: auto; /*可显示滚动条*/
- }
- .title {
- padding:4px;
- font-size: 14px;
- }
- .title img {
- width: 16px;
- height: 16px;
- float: right;
- cursor: pointer;
- }
window.js
- $(document).ready(function() {
- //1. 点击按钮可以在屏幕中间显示一个窗口
- //2. 点击按钮2可以在屏幕的左下角显示一个窗口
- var contentwin = $("#center").mywin({left: "center", top: "center"});
- var leftwin = $("#left").mywin({left: "left", top: "bottom"}, function() {
- leftwin.slideUp("slow");
- } );
- $("#contentpop").click(function(){
- //鼠标点击按钮之后,把id为center的窗口显示在页面中间
- //计算位于屏幕中间的窗口的左边界和上边界的值
- // 浏览器可视区域的宽和高,当前窗口的宽和高
- contentwin.show("slow");
- });
- $("#leftpop").click(function() {
- leftwin.slideDown("slow");
- }
- });
- //position包含两个属性,一个是left,一个是top
- //hidefunc表示执行窗口隐藏的方法
- $.fn.mywin = function(position, hidefunc) {
- if(position && position instanceof Object) {
- var positionleft = position.left;
- var positiontop = position.top;
- var windowobj = $(window);
- var browserwidth = $(window).width();
- var browserheight = $(window).height();
- var scrollLeft = $(window).scrollLeft();
- var scrollTop = $(windwo).scrollTop();
- var currentwin = this;
- var cwinwidth = currentwin.outerWidth(true);
- var cwinheight = currentwin.outerHeight(true);
- var left;
- var top;
- function calLeft(positionleft,scrollLeft,browsewidth,cwinwidth){
- if(positionleft && typeof positionleft == "string") {
- if(positionleft == "center") {
- left = scrollLeft + (browserwidth – cwinwidth) /2;
- }else if (positionleft == "left") {
- left = scrollLeft;
- }else if (positionleft == "right") {
- left = scrollLeft + browserwidth – cwinwidth;
- }else {
- left = scrollLeft + (browserwidth – cwinwidth) /2;
- }
- }else if(positionleft && typeof positionleft == "number" ) {
- left = positionleft;
- }else {
- left = 0;
- }
- currentwin.data("positionleft",positionleft);
- }
- calLeft(positionleft,scrollLeft,browsewidth,cwinwidth)
- function calTop(positiontop,scrollTop,browseheight,cwinwidthheight) {
- if(positiontop && typeof positiontop == "string") {
- if(positiontop == "center") {
- top =scrollTop + (browserheight – cwinheight) / 2;
- }else if (positiontop == "top") {
- top = scrollTop;
- }else if (positiontop == "bottom") {
- top = scrollTop + browseheight – cwinheight;
- }else {
- top =scrollTop + (browserheight – cwinheight) / 2;
- }
- } else if(positionleft && typeof positiontop == "number" ) {
- top = positionleft;
- }else {
- top = 0;
- }
- currentwin.data("positiontop",positiontop);
- }
- calTop(positiontop,scrollTop,browseheight,cwinwidthheight);
- var scrollTimeout;
- $(window).scroll(function() {
- clearTimeout(scrollTimeout);
- scrollTimeout = setTimeout(function() { //延时处理
- var browserwidth = $(window).width();
- var browserheight = $(window).height();
- var scrollLeft = $(window).scrollLeft();
- var scrollTop = $(windwo).scrollTop();
- calLeft(currentwin.data("positionleft"),scrollLeft,browsewidth,cwinwidth);
- calTp[(currentwin.data("positiontop"),scrollLeft,browsheight,cwinheight);
- currentwin.animate({
- left: left,
- top: top;
- },300);
- },300);
- });
- $(window).resize(function(){
- var browserwidth = $(window).width();
- var browserheight = $(window).height();
- var scrollLeft = $(window).scrollLeft();
- var scrollTop = $(windwo).scrollTop();
- calLeft(currentwin.data("positionleft"),scrollLeft,browsewidth,cwinwidth);
- calTp[(currentwin.data("positiontop"),scrollLeft,browsheight,cwinheight);
- currentwin.animate({
- left: left,
- top: top;
- },300);
- });
- currentwin.css("left",left).css("top",top);
- currentwin.children(".title").children("img").click(function(){
- if(!hidefunc) {
- currentwin.hide("slow");
- }else {
- hidefunc();
- }
- });
- return currentwin; //返回当前对象,以便可以级联的执行其他方法
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20