This article provides a summary of 10 very practical and simple jquery special effects that can be used in almost all projects. We recommend them to you. If you need them, you can refer to them.
1. 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");
2. Open the link in a new window (target = "blank ")
$('a[@rel$='external']').click(function(){ this.target = "_blank";}); /* Usage: catswhocode.com*/
3. 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');
4. 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; });});
5. 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 });});
6. Create a column with a height
var max_height = 0;$("p.col").each(function(){ if ($(this).height() > max_height) { max_height = $(this).height(); }});$("p.col").height(max_height);
7. Enable HTML5 support in 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 the head
8. test whether the browser supports certain CSS3 attributes.
var supports = (function() { var p = document.createElement('p'), vendors = 'Khtml Ms O Moz Webkit'.split(' '), len = vendors.length; return function(prop) { if ( prop in p.style ) return true; prop = prop.replace(/^[a-z]/, function(val) { return val.toUpperCase(); }); while(len--) { if ( vendors[len] + prop in p.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';
9. Get 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;}
10. Disable the form's enter key submission
$("#form").keypress(function(e) { if (e.which == 13) { return false; }});