10 great jquery Code Snippets _jquery

Source: Internet
Author: User

Picture Pre-loading

(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"); 

Open a link in a new window (target= "blank")

$ (' a[@rel $= ' External ']. Click (function () { 
   this.target = "_blank"; 
}); 
  /* Usage: 
  <a href= "http://www.catswhocode.com" rel= "external" >catswhocode.com</a> 
* 

Add class for body when JavaScript is supported

/* The code is only 1 lines, but the simplest way to detect if the browser supports JavaScript is to add a HASJS class/$ (' body ') to the BODY element if you support JavaScript 
. addclass (' Hasjs '); 

Smooth scrolling pages to an anchor point

$ (document). Ready (function () { 
  $ ("A.toplink"). Click (function () { 
    $ (HTML, body). Animate ({ 
      scrolltop: $ ($ (this). attr ("href")). Offset (). Top + "px" 
    }, { 
      duration:500, 
      easing: "Swing" 
    }); 
    return false; 
  }); 
 

Fade and Fade When the mouse is sliding

$ (document). Ready (function () { 
  $ (". Thumbs img"). Fadeto ("slow", 0.6);//This sets the opacity of the thumbs to Fade Dow N to 60% when the page loads 
 
  $ (". Thumbs img"). Hover (function () { 
    $ (this). Fadeto ("Slow", 1.0);//this should set th e opacity to 100% on hover 
  },function () { 
    $ (a). Fadeto ("slow", 0.6);//This should set the opacity back to 60% O n mouseout 
  }); 
 

Make a column of equal height

var max_height = 0; 
$ ("Div.col"). each (the function () { 
  if ($ (this). Height () > max_height) {max_height = $ (this). Height ();} 
}); 
$ ("Div.col"). Height (max_height); 

Enable HTML5 support on some old browsers

(function () { 
  if (!/* @cc_on!@*/0) return 
    ; 
  var e = "Abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header, Hgroup,mark,menu,meter,nav,output,progress,section,time,video ". Split (', '), I=e.length;while (i--) { Document.createelement (E[i])} 
) () 
 
//Then introduce the JS <!--in head 
[if Lt IE 9]> 
<script src= "http:// Html5shim.googlecode.com/svn/trunk/html5.js "></script> 
<![ Endif]--> 

Test whether the browser supports certain CSS3 properties

var supports = (function () { 
  var div = document.createelement (' div '), 
   vendors = ' khtml Ms O Moz Webkit '. Split (") , 
   len = vendors.length; 
 
  return function (prop) { 
   if (prop into Div.style) return true; 
 
   Prop = Prop.replace (/^[a-z]/, function (val) {return 
     val.touppercase (); 
   }); 
 
   while (len--) { 
     if (Vendors[len] + prop in Div.style) { 
      //browser supports. Do what your need. 
      Or use a bang (!) to test if the browser doesn ' t. 
      return true; 
     } 
   return false;} 
) (); 
 
if (Supports (' Textshadow ')) { 
  Document.documentElement.className + = ' Textshadow '; 

Gets the parameters passed in the URL

$.urlparam = function (name) { 
  var results = new RegExp (' [\\?&] ' + name + ' = ([^&#]*) '). EXEC ( WINDOW.LOCATION.HREF); 
  if (!results) {return 0;} 
  return Results[1] | | 0; 
} 

To disable the form's enter key submission

$ ("#form"). KeyPress (function (e) { 
 if (E.which =) {return 
  false; 
 } 
}); 

Before the "direct use of the 15 pieces of jquery Code Snippets" article is very good, do not know the collection? Now, one more, just as good!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.