zoukankan      html  css  js  c++  java
  • 开发中可能会用到的几个 jQuery 小提示和技巧

    1) 禁止右键

     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 });

    2) 文本缩放

    $(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);
      });
    });

    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) 样式表切换

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

    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 });

    5) 回到顶部

    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 });

    预加载图片

    这个图片预加载片段让你能够快速的预先载入图片,不需要等待。代码如下:
    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…');
    });

     

  • 相关阅读:
    git删除目录,且保留本地的
    gitpush 免密码
    git常用操作
    ubuntu安装Nodejs
    ubuntu如何配置samba
    用AI将png转成svg做字符图标教程
    windows server 2012设置远程连接断开后自动注销
    windows 2012执行计划任务错误:操作员或系统管理员拒绝了请求(0x800710E0)
    删除节点
    代理 XP”组件已作为此服务器安全配置的一部分被关闭。系统管理员可以使用 sp_configure 来启用“代理 XP”。
  • 原文地址:https://www.cnblogs.com/jami918/p/3584887.html
Copyright © 2011-2022 走看看