首页 > Web开发 > 详细

开发中可能会用到的几个 jQuery 小提示和技巧

时间:2014-03-07 15:36:25      阅读:664      评论:0      收藏:0      [点我收藏+]

1) 禁止右键

bubuko.com,布布扣
 1 $(document).ready(function() {
 2     //catch the right-click context menu
 3     $(document).bind("contextmenu",function(e) {                 
 4         //warning prompt - optional
 5         alert("No right-clicking!");
 6  
 7         //delete the default context menu
 8         return false;
 9     });
10 });
bubuko.com,布布扣

 

 

2) 文本缩放

 

bubuko.com,布布扣
$(document).ready(function() {
    //find the current font size
    var originalFontSize = $(‘html‘).css(‘font-size‘);
 
    //Increase the text size
    $(".increaseFont").click(function() {
        var currentFontSize = $(‘html‘).css(‘font-size‘);
        var currentFontSizeNumber = parseFloat(currentFontSize, 10);
 
        var newFontSize = currentFontSizeNumber*1.2;
        $(‘html‘).css(‘font-size‘, newFontSize);
        return false;
    });
 
    //Decrease the Text Size
    $(".decreaseFont").click(function() {
        var currentFontSize = $(‘html‘).css(‘font-size‘);
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
 
        var newFontSize = currentFontSizeNum*0.8;
        $(‘html‘).css(‘font-size‘, newFontSize);
        return false;
    });
 
    // Reset Font Size
    $(".resetFont").click(function(){
    $(‘html‘).css(‘font-size‘, originalFontSize);
  });
});
bubuko.com,布布扣

 

3) 在新窗口打开链接

 

1 $(document).ready(function() {
2     //select all anchor tags that have http in the href
3     //and apply the target=_blank
4     $("a[href^=‘http‘]").attr(‘target‘,‘_blank‘);
5 });

 

 

4) 样式表切换

  你知道网站换肤是怎么做的吗?下面的代码可以帮助你实现样式表切换功能,如下:

 

bubuko.com,布布扣
1 $(document).ready(function() {
2     $("a.cssSwap").click(function() {
3         //swap the link rel attribute with the value in the rel   
4         $(‘link[rel=stylesheet]‘).attr(‘href‘ , $(this).attr(‘rel‘));
5     });
6 });
bubuko.com,布布扣

 

5) 回到顶部

bubuko.com,布布扣
1 $(document).ready(function() {
2     //when the id="top" link is clicked
3     $(‘#top‘).click(function() {
4         //scoll the page back to the top
5         $(document).scrollTo(0,500);
6     }
7 });
bubuko.com,布布扣

 

 

 

 

预加载图片

这个图片预加载片段让你能够快速的预先载入图片,不需要等待。代码如下:
1
2
3
4
5
jQuery.preloadImagesInWebPage = function() {
    for(var ctr = 0; ctr<arguments.length; ctr++){
        jQuery("").attr("src", arguments[ctr]);
    }
}

  调用方法:

1
$.preloadImages("image1.gif", "image2.gif", "image3.gif");

  判断图片是否已加载:

1
2
3
$(‘#imageObject‘).attr(‘src‘, ‘image1.gif‘).load(function() {
    alert(‘The image has been loaded…‘);
});

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

开发中可能会用到的几个 jQuery 小提示和技巧,布布扣,bubuko.com

开发中可能会用到的几个 jQuery 小提示和技巧

原文:http://www.cnblogs.com/jami918/p/3584887.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!