Thursday, August 20, 2015

Useful Jquery Tips



1) Preloading images

$(document).ready(function() {
jQuery.preloadImages = function()
{
  for(var i = 0; i
").attr("src", arguments[i]);
  }
}
// how to use
$.preloadImages("image1.jpg");
});
2) CSS Styleswitcher 


$(document).ready(function() {
$("a.Styleswitcher").click(function() {
//swicth the LINK REL attribute with the value in A REL attribute
$('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
});
// how to use
// place this in your header - the links
Default Theme
Red Theme
Blue Theme
});
3) Smooth(animated) page scroll 


$(document).ready(function() {
$('a[href*=#]').click(function() {
if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
&& location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
  var targetOffset = $target.offset().top;
  $('html,body')
  .animate({scrollTop: targetOffset}, 900);
    return false;
   }
  }
  });
// how to use
// place this where you want to scroll to - the link
go to top
});
4) Get the mouse cursor x and y axis

$(document).ready(function() {
   $().mousemove(function(e){
     //display the x and y axis values inside the div with the id XY
    $('#XY').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
  });
});
5) Disable right-click 

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
        return false;
    });
});

Popular Posts