jQuery实例
左侧菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>左菜单</title> <script src="jquery-1.12.4.js"></script> <style> .menu{ height: 500px; 30%; background-color: #0e90d2; float: left; } .content{ height: 500px; 70%; background-color: #303a40; float: left; } .title{ line-height: 50px; background-color: #8c8c8c; color: #ffff99; } .hide{ display: none; } </style> </head> <body> <div class="outer"> <div class="menu"> <div class="item"> <div class="title" onclick="show(this);">菜单一</div> <div class="con"> <div>8888</div> <div>8888</div> <div>8888</div> <div>8888</div> </div> </div> <div class="item"> <div class="title" onclick="show(this);">菜单二</div> <div class="con"> <div>8888</div> <div>8888</div> <div>8888</div> <div>8888</div> </div> </div> <div class="item"> <div class="title" onclick="show(this);">菜单三</div> <div class="con"> <div>8888</div> <div>8888</div> <div>8888</div> <div>8888</div> </div> </div> </div> <div class="content"></div> </div> <script> function show(self){ $(self).next().removeClass("hide") $(self).parent().siblings().children(".con").addClass("hide") } </script> </body> </html>
TAB菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery-1.12.4.js"></script> <style> *{ margin: 0px; padding: 0px; } .tab_outer{ margin:0 auto; 60%; } .menu{ background-color: #ffff99; line-height: 40px; cursor:pointer; //鼠标变小手 } .menu li{ display: inline-block; } .menu a{ border-right:1px solid rebeccapurple ; padding: 11px; } .content{ background-color: rosybrown; border: 1px solid greenyellow; height: 300px; } .hide{ display: none; } .content{ background-color: #2459a2; color: #0e90d2; border-top:solid 2px darkgreen; } </style> </head> <body> <div class="tab_outer"> <ul class="menu"> <li xxx="c1" class="current" onclick="tab(this);">菜单一</li> <li xxx="c2" onclick="tab(this);">菜单二</li> <li xxx="c3" onclick="tab(this);">菜单三</li> </ul> <div class="content"> <div id="c1">内容一</div> <div id="c2" class="hide">内容二</div> <div id="c3" class="hide">内容三</div> </div> </div> <script> function tab(self){ var index=$(self).attr("xxx"); $("#"+index).removeClass("hide").siblings().addClass("hide"); $(self).addClass("current").siblings().removeClass("current"); } </script> </body> </html>
滚动菜单
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> body{ margin: 0px; } img { border: 0; } ul{ padding: 0; margin: 0; list-style: none; } .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } .wrap{ 980px; margin: 0 auto; } .pg-header{ background-color: #303a40; -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2); -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2); box-shadow: 0 2px 5px rgba(0,0,0,.2); } .pg-header .logo{ float: left; padding:5px 10px 5px 0px; } .pg-header .logo img{ vertical-align: middle; 110px; height: 40px; } .pg-header .nav{ line-height: 50px; } .pg-header .nav ul li{ float: left; } .pg-header .nav ul li a{ display: block; color: #ccc; padding: 0 20px; text-decoration: none; font-size: 14px; } .pg-header .nav ul li a:hover{ color: #fff; background-color: #425a66; } .pg-body{ } .pg-body .catalog{ position: absolute; top:60px; 200px; background-color: #fafafa; bottom: 0px; } .pg-body .catalog.fixed{ position: fixed; top:10px; } .pg-body .catalog .catalog-item.active{ color: #fff; background-color: #425a66; } .pg-body .content{ position: absolute; top:60px; 700px; margin-left: 210px; background-color: #fafafa; overflow: auto; } .pg-body .content .section{ height: 500px; } </style> </head> <body> <div class="pg-header"> <div class="wrap clearfix"> <div class="logo"> <a href="#"> <img src="3.jpg"> </a> </div> <div class="nav"> <ul> <li> <a href="#">首页</a> </li> <li> <a href="#">功能一</a> </li> <li> <a href="#">功能二</a> </li> </ul> </div> </div> </div> <div class="pg-body"> <div class="wrap"> <div class="catalog"> <div class="catalog-item" auto-to="function1"><a>第1张</a></div> <div class="catalog-item" auto-to="function2"><a>第2张</a></div> <div class="catalog-item" auto-to="function3"><a>第3张</a></div> </div> <div class="content"> <div menu="function1" class="section"> <h1>第一章</h1> </div> <div menu="function2" class="section"> <h1>第二章</h1> </div> <div menu="function3" class="section"> <h1>第三章</h1> </div> </div> </div> </div> <script type="text/javascript" src="jquery-2.1.4.min.js"></script> <script type="text/javascript"> window.onscroll=function () { var ws=$(window).scrollTop(); if (ws>50){ $(".catalog").addClass("fixed"); } else { $(".catalog").removeClass("fixed"); } if($(document).height()==$(window).height()+ws){ $(".catalog").children(":last").css("fontSize","40px").siblings().css("fontSize","16px") return; } $(".content").children().each(function () { var offset=$(this).offset().top; var total=$(this).height()+offset; if (ws>offset && ws<total){ var index=$(this).attr("menu"); var new_index="[auto-to="+index+"]"; console.log(new_index) $(new_index).css("fontSize","40px").siblings().css("fontSize","15px"); } }); }; </script> </body> </html>
动画显示隐藏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #flipshow,#content,#fliphide,#toggle{ padding: 5px; text-align: center; background-color: #0e90d2; border: solid 1px rebeccapurple; } #content{ padding: 100px; display: none; background-color: #c70048; } </style> </head> <body> <div id="flipshow">显示</div> <div id="fliphide">隐藏</div> <div id="toggle">toggle</div> <div id="content">hello world</div> <script src="jquery-1.12.4.js"></script> <script> $(document).ready(function(){ $("#flipshow").click(function(){ $("#content").slideDown(1000); }); $("#fliphide").click(function(){ $("#content").slideUp(1000); }); $("#toggle").click(function(){ $("#content").slideToggle(1000); }); }); </script> </body> </html>
淡入淡出
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <button id="in">fadein</button> <button id="out">fadeout</button> <button id="toggle">fadetoggle</button> <button id="fadeto">fadeto</button> <div id="id1" style="display:none; 80px;height: 80px;background-color: blueviolet"></div> <div id="id2" style="display:none; 80px;height: 80px;background-color: orangered "></div> <div id="id3" style="display:none; 80px;height: 80px;background-color: darkgreen "></div> <script src="jquery-1.12.4.js"></script> <script> $(document).ready(function(){ $("#in").click(function(){ $("#id1").fadeIn(1000); $("#id2").fadeIn(1000); $("#id3").fadeIn(1000); }); $("#out").click(function(){ $("#id1").fadeOut(1000); $("#id2").fadeOut(1000); $("#id3").fadeOut(1000); }); $("#toggle").click(function(){ $("#id1").fadeToggle(1000); $("#id2").fadeToggle(1000); $("#id3").fadeToggle(1000); }); $("#fadeto").click(function(){ $("#id1").fadeTo(1000,0.4); $("#id2").fadeTo(1000,0.5); $("#id3").fadeTo(1000,0); }); }); </script> </body> </html>
隐藏显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link type="text/css" rel="stylesheet" href="style.css"> </head> <body> <!--1 隐藏与显示--> <!--2 淡入淡出--> <!--3 滑动--> <!--4 效果-回调:每一个动画执行完毕之后所能执行的函数方法或者所能做的一件事--> <p>hello</p> <button id="hide">隐藏</button> <button id="show">显示</button> <button id="toggle">隐藏/显示</button> <script src="jquery-1.12.4.js"></script> <script> /** * Created by yuan on 16/5/5. */ $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(1000); }); $("#show").click(function(){ $("p").show(1000); }); //用于切换被选元素的 hide() 与 show() 方法。 $("#toggle").click(function(){ $("p").toggle(1000); }) for (var i= 0;i<7;i++){ // 颠倒了常规的$(A).append(B)的操作,即不是把B追加到A中,而是把A追加到B中。 $("<div>").appendTo(document.body); } $("div").click(function(){ $(this).hide(2000); }); }); </script> </body> </html>
返回顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body{ margin: 0px; } .returnTop{ height: 60px; 100px; background-color: darkgray; position: fixed; right: 0; bottom: 0; color: greenyellow; line-height: 60px; text-align: center; } .div1{ background-color: orchid; font-size: 5px; overflow: auto; 500px; } .div2{ background-color: darkcyan; } .div3{ background-color: aqua; } .div{ height: 300px; 70%; } .hide{ display: none; } .returnTop{ cursor:pointer; } </style> </head> <body> <div class="div1 div"> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> <p>hello</p> </div> <div class="div2 div"></div> <div class="div3 div"></div> <div class="returnTop hide" onclick="returnTop();">返回顶部</div> <script src="jquery-1.12.4.js"></script> <script> window.onscroll=function(){ var current=$(window).scrollTop(); console.log(current); if (current>100){ $(".returnTop").removeClass("hide"); } else { $(".returnTop").addClass("hide") } }; function returnTop(){ $(".div1").scrollTop(0); $(window).scrollTop(0) }; </script> </body> </html>
添加、删除
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="outer"> <div class="section"> <div class="icons" style="display: inline-block"> <a onclick="add(this)"><button>+</button></a> </div> <div class="inputs" style="display: inline-block"> <input type="checkbox"> <input type="text" value="IP"> </div> </div> </div> <script src="../day15/jquery-1.12.4.js"></script> <script> function add(self){ //添加函数 var item=$(self).parent().parent().clone(); //克隆找寻的标签 $(".outer").append(item); //添加到标签的后面 item.find("a").children().text("-"); //把该标签的‘+’号改‘-’号 item.find("a").attr("onclick","remove8(this);") //绑定删除标签函数 } function remove8(self){ //删除函数 $(self).parent().parent().remove() } </script> </body> </html>
模态对话框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .shade{ position: fixed; left: 0; top: 0; right: 0; bottom: 0; background: rgba(0,0,0,.6) ; z-index: 20; } .modal{ position: fixed; left: 50%; top: 50%; height: 300px; 400px; margin-top: -150px; margin-left: -200px; z-index: 30; border: 1px solid #ddd; background-color: white; text-align: center; } .hide{ display: none; } </style> </head> <body> <div> <input type="button" value="添加" onclick="Add();" /> <table border="1"> <thead> <tr> <th >主机名</th> <th >IP</th> <th >端口</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td target="host">c1.com</td> <td target="ip">1.1.1.1</td> <td target="port">8888</td> <td onclick="Edit(this);">Edit</td> </tr> <tr> <td target="host">c2.com</td> <td target="ip">1.1.1.1</td> <td target="port">8888</td> <td onclick="Edit(this);">Edit</td> </tr> <tr> <td target="host">c3.com</td> <td target="ip">1.1.1.1</td> <td target="port">8888</td> <td onclick="Edit(this);">Edit</td> </tr> <tr> <td target="host">c4.com</td> <td target="ip">1.1.1.1</td> <td target="port">8888</td> <td onclick="Edit(this);">Edit</td> </tr> </tbody> </table> </div> <div class="shade hide"></div> <div class="modal hide"> <form action="" method="get"> <p>主机名<input type="text" id="host" name="host"></p> <p>IP <input type="text" id='ip' name="ip"></p> <p>端口 <input type="text" id='port' name="port"></p> <input type="submit" value="提交" onclick="return SubmitForm();"> <input type="button" value="取消" onclick="HideModal();"> </form> </div> <script src="jquery-1.12.4.js"></script> <script> function SubmitForm(){ var flag = true; $(".modal").find('input[type="text"]').each(function(){ var value = $(this).val(); if(value.trim().length ==0){ flag = false; return false; } }); return flag; } function Edit(ths){ //ths代指当前点击标签 //$(ths)表示jquery对象 $('.shade,.modal').removeClass('hide'); var prevList = $(ths).prevAll(); prevList.each(function(){ var prevList = $(this).text(); var target = $(this).attr('target'); $("#"+target).val(text); }); } function HideModal(){ $('.shade,.modal').addClass("hide"); $(".modal").find("input[text]").val(""); } function Add(){ $('.shade,.modal').removeClass('hide'); } </script> </body> </html>
正反选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正反选</title> </head> <body> <button onclick="selectAll();">全选</button> <button onclick="cancel();">取消</button> <button onclick="reverse();">反选</button> <table border="1"> <tr> <td><input type="checkbox"></td> <td>8888</td> </tr> <tr> <td><input type="checkbox"></td> <td>8888</td> </tr> <tr> <td><input type="checkbox"></td> <td>8888</td> </tr> <tr> <td><input type="checkbox"></td> <td>8888</td> </tr> </table> <script src="jquery-1.12.4.js"></script> <script> function selectAll() { $("table :checkbox").prop("checked",true); } function cancel() { $("table :checkbox").prop("checked",false); } function reverse() { $("table :checkbox").each(function(){ if ($(this).prop("checked")){ $(this).prop("checked",false); } else { $(this).prop("checked",true); } })}; </script> </body> </html>
轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0px; } ul li{ list-style: none; } .outer{ height:454px; 730px; border: dashed cadetblue 5px; margin: 0 auto; position: relative; } .num{ position: absolute; left: 0; /*top: 0;*/ bottom: 10px; font-size: 0px; text-align: center; 100%; } .num li{ height: 20px; 20px; background-color: darkgray; border-radius: 60%; text-align: center; line-height: 20px; display: inline-block; font-size: 16px; margin: 5px; cursor: pointer; } .outer .img li{ position: absolute; left:0 ; top: 0; height: 454px; 730px; } .button{ height: 60px; 40px; background-color: darkgrey; position: absolute; /*left: 0px;*/ top: 50%; margin-top: -30px; opacity: 0.6; font-size: 40px; font-weight: bolder; text-align: center; line-height: 60px; display: none; } .btn_right{ right: 0; } .outer:hover .button{ display: block; } .outer .num li.current{ background-color: red; } </style> </head> <body> <div class="outer"> <ul class="img"> <li><img src="1.jpg"></li> <li><img src="2.jpg"></li> <li><img src="3.jpg"></li> <li><img src="4.jpg"></li> <li><img src="5.jpg"></li> </ul> <ul class="num"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <div class="btn_left button"> < </div> <div class="btn_right button"> > </div> </div> <script src="jquery-1.12.4.js"></script> <script> $(function () { //手动轮播 $(".num li").first().addClass("current"); $(".num li").mouseover(function () { $(this).addClass("current").siblings().removeClass("current"); var index=$(this).index(); i=index; $(".img li").eq(index).stop().fadeIn(1000).siblings().stop().fadeOut(1000); }); //自动轮播 i=0; var time=setInterval(move,1500); function move() { i++; if(i==5){ i=0; } $(".num li").eq(i).addClass("current").siblings().removeClass("current"); $(".img li").eq(i).stop().fadeIn(1000).siblings().stop().fadeOut(1000); } //暂停轮播 $(".outer").hover(function () { clearInterval(time); },function () { time=setInterval(move,1500); }); //左右按钮移动 $(".btn_right").click(function () { move(); }); $(".btn_left").click(function () { if (i==0){ i=5; } i=i-2; move(); }) }) </script> </body> </html>
拖动面板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #mb{ border: 1px solid #ddd; 600px; position: absolute; } #title{ background-color: black; height: 40px; color: white; text-align: center; } #nr{ height: 300px; background-color: #8c8c8c; text-align: center; } </style> </head> <body> <div id="mb"> <div id="title"> 标题 </div> <div id="nr"> 内容 </div> </div> <script type="text/javascript" src="jquery-1.12.4.js"></script> <script> $(function(){ // 页面加载完成之后自动执行 $('#title').mouseover(function(){ $(this).css('cursor','move'); }).mousedown(function(e){ //console.log($(this).offset()); var _event = e || window.event; // 原始鼠标横纵坐标位置 var ord_x = _event.clientX; var ord_y = _event.clientY; var parent_left = $(this).parent().offset().left; var parent_top = $(this).parent().offset().top; $(this).bind('mousemove', function(e){ var _new_event = e || window.event; var new_x = _new_event.clientX; var new_y = _new_event.clientY; var x = parent_left + (new_x - ord_x); var y = parent_top + (new_y - ord_y); $(this).parent().css('left',x+'px'); $(this).parent().css('top',y+'px'); }) }).mouseup(function(){ $(this).unbind('mousemove'); }); }) </script> </body> </html>
放大镜
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width"> <meta http-equiv="X-UA-Compatible" content="IE=8"> <title>购物商城</title> <style> *{ margin: 0px; padding: 0px; } .outer{ position:relative; 350px; height:350px; /*margin-bottom:5px;*/ border:5px solid rebeccapurple; } .outer .small-box{ position: relative; z-index: 1; } .outer .small-box .mark{ position: absolute; display: block; 350px; height: 350px; background-color: #fff; filter: alpha(opacity=0); opacity: 0; z-index: 10; } .outer .small-box .float-box{ display: none; 175px; height: 175px; position: absolute; background: #DAEEFC; filter: alpha(opacity=40); opacity: 0.4; } .outer .big-box{ /*display:none;*/ position: absolute; top: 0; left: 351px; 400px; height: 400px; overflow: hidden; border: 5px dashed #ccc; z-index: 1; } .outer .big-box img{ position: absolute; z-index: 5 } </style> </head> <body> <div class="outer"> <div class="small-box"> <div class="mark"></div> <div class="float-box" ></div> <img width="350" height="350" src="../day15/small.jpg"> </div> <div class="big-box"> <img width="800px" height="800px" src="../day15/big.jpg" > </div> </div> <script src="../day15/jquery-1.12.4.js"></script> <script> $(function(){ $(".mark").mouseover(function () { $(".float-box").css("display","block") $(".big-box").css("display","block") }); $(".mark").mouseout(function () { $(".float-box").css("display","none") $(".big-box").css("display","none") }); $(".mark").mousemove(function (e) { var _event = e || window.event; //兼容多个浏览器的event参数模式 var float_box_width = $(".float-box")[0].offsetWidth; var float_box_height = $(".float-box")[0].offsetHeight;//175,175 var float_box_width_half = float_box_width / 2; var float_box_height_half = float_box_height/ 2;//87.5,87.5 var small_box_width = $(".outer")[0].offsetWidth; var small_box_height = $(".outer")[0].offsetHeight;//360,360 // 鼠标点距离左边界的长度与float应该与左边界的距离差半个float的width,height同理 var mouse_left = _event.clientX - float_box_width_half; var mouse_top = _event.clientY - float_box_height_half; console.log(_event.clientX,_event.clientY); console.log(small_box_width - float_box_width) if (mouse_left < 0) { mouse_left = 0; } else if (mouse_left > small_box_width - float_box_width) { mouse_left = small_box_width - float_box_width; } if (mouse_top < 0) { mouse_top = 0; } else if (mouse_top > small_box_height - float_box_height) { mouse_top = small_box_height - float_box_height; } $(".float-box").css("left",mouse_left + "px"); $(".float-box").css("top",mouse_top + "px"); //小图移动一个像素,对应大图应该移动几个像素? //思路:用大图可移动的像素数/小图可移动的像素数 var percentX = ($(".big-box img")[0].offsetWidth - $(".big-box")[0].offsetWidth) / (small_box_width - float_box_width); var percentY = ($(".big-box img")[0].offsetHeight - $(".big-box")[0].offsetHeight) / (small_box_height - float_box_height); console.log($(".big-box img")[0].offsetWidth,$(".big-box")[0].offsetWidth,small_box_width,float_box_width) console.log(percentX,percentY) $(".big-box img").css("left",-percentX * mouse_left + "px"); $(".big-box img").css("top",-percentY * mouse_top + "px") }) }) </script> </body> </html>
购物菜单
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width"> <meta http-equiv="X-UA-Compatible" content="IE=8"> <title>购物商城</title> <style> *{ margin: 0px; padding: 0px; } .outer{ position:relative; 350px; height:350px; /*margin-bottom:5px;*/ border:5px solid rebeccapurple; } .outer .small-box{ position: relative; z-index: 1; } .outer .small-box .mark{ position: absolute; display: block; 350px; height: 350px; background-color: #fff; filter: alpha(opacity=0); opacity: 0; z-index: 10; } .outer .small-box .float-box{ display: none; 175px; height: 175px; position: absolute; background: #DAEEFC; filter: alpha(opacity=40); opacity: 0.4; } .outer .big-box{ /*display:none;*/ position: absolute; top: 0; left: 351px; 400px; height: 400px; overflow: hidden; border: 5px dashed #ccc; z-index: 1; } .outer .big-box img{ position: absolute; z-index: 5 } </style> </head> <body> <div class="outer"> <div class="small-box"> <div class="mark"></div> <div class="float-box" ></div> <img width="350" height="350" src="../day15/small.jpg"> </div> <div class="big-box"> <img width="800px" height="800px" src="../day15/big.jpg" > </div> </div> <script src="../day15/jquery-1.12.4.js"></script> <script> $(function(){ $(".mark").mouseover(function () { $(".float-box").css("display","block") $(".big-box").css("display","block") }); $(".mark").mouseout(function () { $(".float-box").css("display","none") $(".big-box").css("display","none") }); $(".mark").mousemove(function (e) { var _event = e || window.event; //兼容多个浏览器的event参数模式 var float_box_width = $(".float-box")[0].offsetWidth; var float_box_height = $(".float-box")[0].offsetHeight;//175,175 var float_box_width_half = float_box_width / 2; var float_box_height_half = float_box_height/ 2;//87.5,87.5 var small_box_width = $(".outer")[0].offsetWidth; var small_box_height = $(".outer")[0].offsetHeight;//360,360 // 鼠标点距离左边界的长度与float应该与左边界的距离差半个float的width,height同理 var mouse_left = _event.clientX - float_box_width_half; var mouse_top = _event.clientY - float_box_height_half; console.log(_event.clientX,_event.clientY); console.log(small_box_width - float_box_width) if (mouse_left < 0) { mouse_left = 0; } else if (mouse_left > small_box_width - float_box_width) { mouse_left = small_box_width - float_box_width; } if (mouse_top < 0) { mouse_top = 0; } else if (mouse_top > small_box_height - float_box_height) { mouse_top = small_box_height - float_box_height; } $(".float-box").css("left",mouse_left + "px"); $(".float-box").css("top",mouse_top + "px"); //小图移动一个像素,对应大图应该移动几个像素? //思路:用大图可移动的像素数/小图可移动的像素数 var percentX = ($(".big-box img")[0].offsetWidth - $(".big-box")[0].offsetWidth) / (small_box_width - float_box_width); var percentY = ($(".big-box img")[0].offsetHeight - $(".big-box")[0].offsetHeight) / (small_box_height - float_box_height); console.log($(".big-box img")[0].offsetWidth,$(".big-box")[0].offsetWidth,small_box_width,float_box_width) console.log(percentX,percentY) $(".big-box img").css("left",-percentX * mouse_left + "px"); $(".big-box img").css("top",-percentY * mouse_top + "px") }) }) </script> </body> </html>
编辑框
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> .edit-mode{ background-color: #b3b3b3; padding: 8px; text-decoration: none; color: white; } .editing{ background-color: #f0ad4e; } </style> </head> <body> <div style="padding: 20px"> <input type="button" onclick="CheckAll('#edit_mode', '#tb1');" value="全选" /> <input type="button" onclick="CheckReverse('#edit_mode', '#tb1');" value="反选" /> <input type="button" onclick="CheckCancel('#edit_mode', '#tb1');" value="取消" /> <a id="edit_mode" class="edit-mode" href="javascript:void(0);" onclick="EditMode(this, '#tb1');">进入编辑模式</a> </div> <table border="1"> <thead> <tr> <th>选择</th> <th>主机名</th> <th>端口</th> <th>状态</th> </tr> </thead> <tbody id="tb1"> <tr> <td><input type="checkbox" /></td> <td edit="true">v1</td> <td>v11</td> <td edit="true" edit-type="select" sel-val="1" global-key="STATUS">在线</td> </tr> <tr> <td><input type="checkbox" /></td> <td edit="true">v1</td> <td>v11</td> <td edit="true" edit-type="select" sel-val="2" global-key="STATUS">下线</td> </tr> <tr> <td><input type="checkbox" /></td> <td edit="true">v1</td> <td>v11</td> <td edit="true" edit-type="select" sel-val="1" global-key="STATUS">在线</td> </tr> </tbody> </table> <script type="text/javascript" src="jquery-1.12.4.js"></script> <script> /* 监听是否已经按下control键 */ window.globalCtrlKeyPress = false; window.onkeydown = function(event){ if(event && event.keyCode == 17){ window.globalCtrlKeyPress = true; } }; window.onkeyup = function(event){ if(event && event.keyCode == 17){ window.globalCtrlKeyPress = false; } }; /* 按下Control,联动表格中正在编辑的select */ function MultiSelect(ths){ if(window.globalCtrlKeyPress){ var index = $(ths).parent().index(); var value = $(ths).val(); $(ths).parent().parent().nextAll().find("td input[type='checkbox']:checked").each(function(){ $(this).parent().parent().children().eq(index).children().val(value); }); } } </script> <script type="text/javascript"> $(function(){ BindSingleCheck('#edit_mode', '#tb1'); }); function BindSingleCheck(mode, tb){ $(tb).find(':checkbox').bind('click', function(){ var $tr = $(this).parent().parent(); // 判断是否进入编辑模式 if($(mode).hasClass('editing')){ // 是否已经选中, // 如果没有选中,进入编辑模式 // 如果已经选中,退出编辑模式 // checkbox 默认事件优先于自定义事件 if($(this).prop('checked')){ RowIntoEdit($tr); }else{ RowOutEdit($tr); } } }); } function CreateSelect(attrs,csses,option_dict,item_key,item_value,current_val){ var sel= document.createElement('select'); $.each(attrs,function(k,v){ $(sel).attr(k,v); }); $.each(csses,function(k,v){ $(sel).css(k,v); }); $.each(option_dict,function(k,v){ var opt1=document.createElement('option'); var sel_text = v[item_value]; var sel_value = v[item_key]; if(sel_value==current_val){ $(opt1).text(sel_text).attr('value', sel_value).attr('text', sel_text).attr('selected',true).appendTo($(sel)); }else{ $(opt1).text(sel_text).attr('value', sel_value).attr('text', sel_text).appendTo($(sel)); } }); return sel; } STATUS = [ {'id': 1, 'value': "在线"}, {'id': 2, 'value': "下线"} ]; BUSINESS = [ {'id': 1, 'value': "车上会"}, {'id': 2, 'value': "二手车"} ]; function RowIntoEdit($tr){ $tr.children().each(function(){ if($(this).attr('edit') == "true"){ if($(this).attr('edit-type') == "select"){ var select_val = $(this).attr('sel-val'); var global_key = $(this).attr('global-key'); // "STATUS" window[global_key] // 生成select标签,并且将其设置默认值 select_val var selelct_tag = CreateSelect({"onchange": "MultiSelect(this);"}, {}, window[global_key], 'id', 'value', select_val); $(this).html(selelct_tag); }else{ var orgin_value = $(this).text(); var temp = "<input value='"+ orgin_value+"' />"; $(this).html(temp); } } }); } function RowOutEdit($tr){ $tr.children().each(function(){ if($(this).attr('edit') == "true"){ if($(this).attr('edit-type') == "select"){ var new_val = $(this).children(':first').val(); var new_text = $(this).children(':first').find("option[value='"+new_val+"']").text(); $(this).attr('sel-val', new_val); $(this).text(new_text); }else{ var orgin_value = $(this).children().first().val(); $(this).text(orgin_value); } } }); } function CheckAll(mode, tb){ // mode= #edit_mode => 用于检测用户是否点击进入编辑模式 // tb = #tb1 => table中的tbody if($(mode).hasClass('editing')){ $(tb).children().each(function(){ // tr = $(this); => 循环的当前行 var tr = $(this); var check_box = tr.children().first().find(':checkbox'); if(check_box.prop('checked')){ }else{ check_box.prop('checked',true); //当前行进入编辑模式 RowIntoEdit(tr); } }); }else{ $(tb).find(':checkbox').prop('checked', true); } } function CheckReverse(mode, tb){ if($(mode).hasClass('editing')){ $(tb).children().each(function(){ var tr = $(this); var check_box = tr.children().first().find(':checkbox'); if(check_box.prop('checked')){ check_box.prop('checked',false); RowOutEdit(tr); }else{ check_box.prop('checked',true); RowIntoEdit(tr); } }); }else{ // $(tb).children().each(function(){ var tr = $(this); var check_box = tr.children().first().find(':checkbox'); if(check_box.prop('checked')){ check_box.prop('checked',false); }else{ check_box.prop('checked',true); } }); } } function CheckCancel(mode, tb){ // mode= #edit_mode => 用于检测用户是否点击进入编辑模式 // tb = #tb1 => table中的tbody if($(mode).hasClass('editing')){ $(tb).children().each(function(){ var tr = $(this); var check_box = tr.children().first().find(':checkbox'); if(check_box.prop('checked')){ check_box.prop('checked',false); // 当前行退出编辑模式 RowOutEdit(tr); }else{ } }); }else{ $(tb).find(':checkbox').prop('checked', false); } } function EditMode(ths, tb){ if($(ths).hasClass('editing')){ $(ths).removeClass('editing'); $(tb).children().each(function(){ var tr = $(this); var check_box = tr.children().first().find(':checkbox'); if(check_box.prop('checked')){ RowOutEdit(tr); }else{ } }); }else{ $(ths).addClass('editing'); $(tb).children().each(function(){ var tr = $(this); var check_box = tr.children().first().find(':checkbox'); if(check_box.prop('checked')){ RowIntoEdit(tr); }else{ } }); } } </script> </body> </html>
未完待续...