22 nov

Pedacitos de código útiles para jQuery

jQuery es un framework muy poderoso a la hora de trabajar con JavaScript. Es por eso que a la hora de desarrollar para web, es una variante muy importante que nos ayudará a ahorrar mucho tiempo escribiendo código. Y esa es la idea de estos 10 códigos para jQuery con funcionalidades que suelen ser generales y muchas veces las necesitamos para varios proyectos.

Precarga de Imágenes

1
2
3
4
5
6
7
8
9
10
11
12
13
(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");

Srolleo suave para anchor

1
2
3
4
5
6
7
8
9
10
11
$(document).ready(function() {
	$("a.topLink").click(function() {
		$("html, body").animate({
			scrollTop: $($(this).attr("href")).offset().top + "px"
		}, {
			duration: 500,
			easing: "swing"
		});
		return false;
	});
});

Columnas de igual alto

1
2
3
4
5
var max_height = 0;
$("div.col").each(function(){
    if ($(this).height() > max_height) { max_height = $(this).height(); }
});
$("div.col").height(max_height);

Habilitar HTML5 en viejos navegadores

1
(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])}})()

Deshabilitar “enter” en los formularios

1
2
3
4
5
$("#form").keypress(function(e) {
  if (e.which == 13) {
    return false;
  }
});

En la fuente original, Cats who code, hay más porciones de códigos útiles para jQuery.

Tags:

Publicado por patricio

Guardado en: Programacion

Esta entrada no tiene comentarios

Tu comentario será moderado la primera vez que lo hagas al igual que si incluyes enlaces. A partir de ahi no será necesario si usas los mismos datos y mantienes la cordura. No se publicarán insultos, difamaciones o faltas de respeto hacia los lectores y comentaristas de este blog.