很棒的 jQuery 代码片段

图片预加载
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }

jQuery.preLoadImages("image1.gif", "/path/to/image2.png");

  

在新窗口打开链接 (target=”blank”)
$('a[@rel$='external']').click(function(){
     this.target = "_blank";
});

/*
   Usage:
   <a href="http://www.catswhocode.com" rel="external">catswhocode.com</a>
*/

  

 1 平滑滚动页面到某个锚点
2 $(document).ready(function() {
3 $("a.topLink").click(function() {
4 $("html, body").animate({
5 scrollTop: $($(this).attr("href")).offset().top + "px"
6 }, {
7 duration: 500,
8 easing: "swing"
9 });
10 return false;
11 });
12 });
 1 鼠标滑动时的渐入和渐出
2 $(document).ready(function(){
3 $(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads
4
5 $(".thumbs img").hover(function(){
6 $(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
7 },function(){
8 $(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout
9 });
10 });
1  获取 URL 中传递的参数
2 $.urlParam = function(name){
3 var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
4 if (!results) { return 0; }
5 return results[1] || 0;
6 }
1 禁用表单的回车键提交
2 $("#form").keypress(function(e) {
3 if (e.which == 13) {
4 return false;
5 }
6 });





posted @ 2012-01-05 10:24  @万里  阅读(130)  评论(0)    收藏  举报