Image 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 the 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 to the body when JavaScript is supported
- /* This code has only one line, but it is the simplest method used to check whether the browser supports JavaScript. if JavaScript is supported, add a hasJS class to the body element */
- $ ('Body'). addClass ('hasjs ');
Smoothly scroll the page to an anchor.
- $(document).ready(function() {
- $("a.topLink").click(function() {
- $("html, body").animate({
- scrollTop: $($(this).attr("href")).offset().top + "px"
- }, {
- duration: 500,
- easing: "swing"
- });
- return false;
- });
- });
Fade-in and fade-out when the mouse slides
- $(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
- });
- });
Create High Columns
- var max_height = 0;
- $("div.col").each(function(){
- if ($(this).height() > max_height) { max_height = $(this).height(); }
- });
- $("div.col").height(max_height);
Enable HTML5 support in some old browsers
- (Function (){
- If (! /* @ Cc_on! @ */0)
- Return;
- Var e = "abbr, article, aside, audio, bb, canvas, datagrid, datalist, details, didio, 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 the head
- <! -- [If lt IE 9]>
- <Script src = "http://html5shim.googlecode.com/svn/trunk/html5.js"> </script>
- <! [Endif] -->
Test whether the browser supports certain CSS3 attributes
- var supports = (function() {
- var div = document.createElement('div'),
- vendors = 'Khtml Ms O Moz Webkit'.split(' '),
- len = vendors.length;
-
- return function(prop) {
- if ( prop in 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 box-shadow. Do what you need.
- // Or use a bang (!) to test if the browser doesn't.
- return true;
- }
- }
- return false;
- };
- })();
-
- if ( supports('textShadow') ) {
- document.documentElement.className += ' textShadow';
Obtain 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;
- }
Forbidden form enter key submission
- $("#form").keypress(function(e) {
- if (e.which == 13) {
- return false;
- }
- });
Original article: http://www.oschina.net/code/snippet_12_7271