2014.01.14 笔记(收集的简单实用的 jQuery 代码片段)
收集几个常用的简捷的代码片段:
1.返回页面顶部:
1 // Back To Top 2 $(document).ready(function(){ 3 $('.top').click(function() { 4 $(document).scrollTo(0,500); 5 }); 6 }); 7 //Create a link defined with the class .top 8 <a href="#" class="top">Back To Top</a>
3.自动替换丢失的图片
1 // Safe Snippet 2 $("img").error(function () { 3 $(this).unbind("error").attr("src", "missing_image.gif"); 4 }); 5 // Persistent Snipper 6 $("img").error(function () { 7 $(this).attr("src", "missing_image.gif"); 8 });
4. 在鼠标悬停时显示淡入/淡出特效
$(document).ready(function(){ $(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads $(".thumbs img").hover(function(){ $(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover }, function(){ $(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout }); });
5.清空表单数据
function clearForm(form) { $(':input', form).each(function() { var type = this.type; var tag = this.tagName.toLowerCase(); if (type == 'text' || type == 'password' || tag == 'textarea') this.value = ""; else if (type == 'checkbox' || type == 'radio') this.checked = false; else if (tag == 'select') this.selectedIndex = -1; }); };
6.图像等比例缩放
1 $(window).bind("load", function() { 2 3 $('#product_cat_list img').each(function() { 4 var maxWidth = 120; 5 var maxHeight = 120; 6 var ratio = 0; 7 var width = $(this).width(); 8 var height = $(this).height(); 9 if(width > maxWidth){ 10 ratio = maxWidth / width; 11 $(this).css("width", maxWidth); 12 $(this).css("height", height * ratio); 13 height = height * ratio; 14 } 15 var width = $(this).width(); 16 var height = $(this).height(); 17 if(height > maxHeight){ 18 ratio = maxHeight / height; 19 $(this).css("height", maxHeight); 20 $(this).css("width", width * ratio); 21 width = width * ratio; 22 } 23 }); 24 25 });
7.让页面中的每个元素都适合在移动设备上展示
1 var scr = document.createElement('script'); 2 scr.setAttribute('src', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js'); 3 document.body.appendChild(scr); 4 scr.onload = function(){ 5 $('div').attr('class', '').attr('id', '').css({ 6 'margin' : 0, 7 'padding' : 0, 8 'width': '100%', 9 'clear':'both' 10 }); //这个我觉得,不需要去掉Div的 class,id ,这样会乱套。。建议保留 Div的class,id。通过重新定义Css也可以 11 };
8. margin:auto实现绝对定位元素的居中
1 .element { 2 width: 600px; height: 400px; 3 position: absolute; left: 0; top: 0; right: 0; bottom: 0; 4 margin: auto; /* 有了这个就自动居中了 */ 5 }
9. 检测密码强度 (在表单功能中,经常会有检测用户输入的密码强度的功能,下面这个代码片段使用正则表达式来检测密码是否足够安全,当然记得在服务端也进行表单验证。)
1 $('#pass').keyup(function(e) { 2 var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g"); 3 var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"); 4 var enoughRegex = new RegExp("(?=.{6,}).*", "g"); 5 if (false == enoughRegex.test($(this).val())) { 6 $('#passstrength').html('More Characters'); 7 } else if (strongRegex.test($(this).val())) { 8 $('#passstrength').className = 'ok'; 9 $('#passstrength').html('Strong!'); 10 } else if (mediumRegex.test($(this).val())) { 11 $('#passstrength').className = 'alert'; 12 $('#passstrength').html('Medium!'); 13 } else { 14 $('#passstrength').className = 'error'; 15 $('#passstrength').html('Weak!'); 16 } 17 return true; 18 });
10. 隔行换色 (在大的列表或表格中,隔行颜色可以大大提高内容的可读性)
1 $('div:odd').css("background-color", "#F4F4F8"); 2 $('div:even').css("background-color", "#EFF1F1");
11. 让整个 Div 可点击
1 //这是实现链接的父 Div 也能够点击的简单方法,HTML 示例代码如下: 2 3 <div class="myBox"> 4 blah blah blah. 5 <a href="http://google.com">link</a> 6 </div> 7 下面的 jQuery 代码让整个 Div 可点击: 8 9 $(".myBox").click(function(){ 10 window.location=$(this).find("a").attr("href"); 11 return false; 12 });
12. 实时监听输入框值变化 (对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有用,在内容修改后立即被触发)
1 $('textarea').bind('input propertychange', function() { 2 $('.msg').html($(this).val().length + ' characters'); 3 });
利用CSS3 中的样式属性设置圆角框,两句代码就能搞定。。
1 <style type="text/css"> 2 #div1 { 3 -moz-border-radius: 10px; 4 -webkit-border-radius: 10px; 5 border:solid 1px #0CC; 6 background:#F9C; 7 width:500px; 8 padding-left:100px; 9 } 10 </style> 11 12 <div id="div1"> aaaaaaaaaaa </div>
效果图: