0
(0)

网页设计中经常会用到JavaScript脚本,能够为我们的网站或网页添加特效,比如我们网站中的回顶部、焦点图、客服代码、选项卡等都可以通过JavaScript脚本实现,这里在网上收集到9个比较常用的JavaScript脚本,以备后用!

1、回顶部JavaScript脚本:

  1. $(“a[href=’#top’]”).click(function() {   
  2.     $(“html, body”).animate({ scrollTop: 0 }, “slow”);   
  3.     return false;   
  4. });  

复制以上代码放在网页的JavaScript标签中,然后在底部添加一个id为“top”的链接就会自动返回到顶部了。

2、复制表单顶部标题到底部:

  1. var $tfoot = $(‘<tfoot></tfoot>’);   
  2. $($(‘thead’).clone(truetrue).children().get().reverse()).each(function(){   
  3.     $tfoot.append($(this));   
  4. });   
  5. $tfoot.insertAfter(‘table thead’);  

3、载入额外的内容:

  1. $(“#content”).load(“somefile.html”function(response, status, xhr) {   
  2.   // error handling   
  3.   if(status == “error”) {   
  4.     $(“#content”).html(“An error occured: “ + xhr.status + ” “ + xhr.statusText);   
  5.   }   
  6. });  

有时候需要为单独的一个div层从外部载入一些额外的数据内容,下面这段短码将会非常有用。

4、设置多列层等高:

  1. var maxheight = 0;   
  2. $(“div.col”).each(function(){   
  3.     if($(this).height() > maxheight) { maxheight = $(this).height(); }   
  4. });   
  5.     
  6. $(“div.col”).height(maxheight);  

在一些布局设计中,有时候需要让两个div层高度相当,下面是采用js方法实现的原理(需要等高的div层设置class为”col”)。

5、定时刷新部分页面的内容:

  1. setInterval(function() {   
  2.     $(“#refresh”).load(location.href+” #refresh>*”,“”);   
  3. }, 10000); // milliseconds to wait  

如果在你的网页上需要定时的刷新一些内容,例如微博消息或者实况转播,为了不让用户繁琐的刷新整个页面,可以采用下面这段代码来定时刷新部分页面内容。

6、预载入图像:

  1. $.preloadImages = function() {   
  2.     for(var i = 0; i<arguments.length; i++) {   
  3.         $(“<img />”).attr(“src”, arguments[i]);   
  4.     }   
  5. }   
  6.     
  7. $(document).ready(function() {   
  8.     $.preloadImages(“hoverimage1.jpg”,“hoverimage2.jpg”);   
  9. });  

有些网站页面打开图像都未载入完毕,还要苦苦等待。下面这段代码实现图像都载入完毕后再打开整个网页。

7、测试密码强度:
这个比较给力,现在很多网站注册的时候都加入了密码强度测试功能,以下代码也简单提供了密码强度测试功能。

HTML代码部分:

  1. <input type=“password” name=“pass” id=“pass” />  
  2. <span id=“passstrength”></span>  

JavaScript脚本代码:

  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. });  

8、自适应缩放图像:
有时候网站上传的图像需要填充整个指定区域,但是有时候图像比例并不恰好合适,缩放后效果不好。一下代码就实现了检测图像比例然后做适当的缩放功能。

  1. $(window).bind(“load”function() {   
  2.     // IMAGE RESIZE   
  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.     
  10.         if(width > maxWidth){   
  11.             ratio = maxWidth / width;   
  12.             $(this).css(“width”, maxWidth);   
  13.             $(this).css(“height”, height * ratio);   
  14.             height = height * ratio;   
  15.         }   
  16.         var width = $(this).width();   
  17.         var height = $(this).height();   
  18.         if(height > maxHeight){   
  19.         ratio = maxHeight / height;   
  20.         $(this).css(“height”, maxHeight);   
  21.         $(this).css(“width”, width * ratio);   
  22.         width = width * ratio;   
  23.      }   
  24. });   
  25. //$(“#contentpage img”).show();   
  26. // IMAGE RESIZE   
  27. });  

9、自动载入内容:
现在很多网站,特别是微博,都不需要翻页的按钮了,直接下拉后会自动载入内容。下面的脚本就是简单实现了个这种效果。

  1. var loading = false;   
  2. $(window).scroll(function(){   
  3.     if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){   
  4.         if(loading == false){   
  5.         loading = true;   
  6.         $(‘#loadingbar’).css(“display”,“block”);   
  7.         $.get(“load.php?start=”+$(‘#loaded_max’).val(), function(loaded){   
  8.             $(‘body’).append(loaded);   
  9.             $(‘#loaded_max’).val(parseInt($(‘#loaded_max’).val())+50);   
  10.             $(‘#loadingbar’).css(“display”,“none”);   
  11.             loading = false;   
  12.         });   
  13.         }   
  14.     }   
  15. });   
  16.     
  17. $(document).ready(function() {   
  18.     $(‘#loaded_max’).val(50);   
  19. });  

注:本文整理至20theme,感谢作者的无私分享!

声明:本站分享的WordPress主题/插件均遵循 GPLv2 许可协议(免费开源),相关介绍资料仅供学习参考,实际版本可能会因版本迭代或开发者调整而产生变化,如程序中涉及有第三方原创图像、设计模板、远程服务等内容,应获得作者授权后方可使用。本站不提供该程序/软件的产品授权与技术服务,亦不收取相关费用。