【css】jquery.pngFix.js解决透明问题

/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.2, 09.03.2009
 * by Andreas Eberhard, [email protected]
 *                      http://jquery.andreaseberhard.de/
 *
 * Copyright (c) 2007 Andreas Eberhard
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Changelog:
 *    09.03.2009 Version 1.2
 *    - Update for jQuery 1.3.x, removed @ from selectors
 *    11.09.2007 Version 1.1
 *    - removed noConflict
 *    - added png-support for input type=image
 *    - 01.08.2007 CSS background-image support extension added by Scott Jehl, [email protected], http://www.filamentgroup.com
 *    31.05.2007 initial Version 1.0
 * --------------------------------------------------------------------
 * @example $(function(){$(document).pngFix();});
 * @desc Fixes all PNG‘s in the document on document.ready
 *
 * jQuery(function(){jQuery(document).pngFix();});
 * @desc Fixes all PNG‘s in the document on document.ready when using noConflict
 *
 * @example $(function(){$(‘div.examples‘).pngFix();});
 * @desc Fixes all PNG‘s within div with class examples
 *
 * @example $(function(){$(‘div.examples‘).pngFix( { blankgif:‘ext.gif‘ } );});
 * @desc Fixes all PNG‘s within div with class examples, provides blank gif for input with png
 * --------------------------------------------------------------------
 */
(function($) {

jQuery.fn.pngFix = function(settings) {

    // Settings
    settings = jQuery.extend({
        blankgif: ‘http://img.mymhotel.com/plugins/pngfix/blank.gif‘
    }, settings);

    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

    if (jQuery.browser.msie && (ie55 || ie6)) {

        //fix images with png-source
        jQuery(this).find("img[src$=.png]").each(function() {

            jQuery(this).attr(‘width‘,jQuery(this).width());
            jQuery(this).attr(‘height‘,jQuery(this).height());

            var prevStyle = ‘‘;
            var strNewHTML = ‘‘;
            var imgId = (jQuery(this).attr(‘id‘)) ? ‘id="‘ + jQuery(this).attr(‘id‘) + ‘" ‘ : ‘‘;
            var imgClass = (jQuery(this).attr(‘class‘)) ? ‘class="‘ + jQuery(this).attr(‘class‘) + ‘" ‘ : ‘‘;
            var imgTitle = (jQuery(this).attr(‘title‘)) ? ‘title="‘ + jQuery(this).attr(‘title‘) + ‘" ‘ : ‘‘;
            var imgAlt = (jQuery(this).attr(‘alt‘)) ? ‘alt="‘ + jQuery(this).attr(‘alt‘) + ‘" ‘ : ‘‘;
            var imgAlign = (jQuery(this).attr(‘align‘)) ? ‘float:‘ + jQuery(this).attr(‘align‘) + ‘;‘ : ‘‘;
            var imgHand = (jQuery(this).parent().attr(‘href‘)) ? ‘cursor:hand;‘ : ‘‘;
            if (this.style.border) {
                prevStyle += ‘border:‘+this.style.border+‘;‘;
                this.style.border = ‘‘;
            }
            if (this.style.padding) {
                prevStyle += ‘padding:‘+this.style.padding+‘;‘;
                this.style.padding = ‘‘;
            }
            if (this.style.margin) {
                prevStyle += ‘margin:‘+this.style.margin+‘;‘;
                this.style.margin = ‘‘;
            }
            var imgStyle = (this.style.cssText);

            strNewHTML += ‘<span ‘+imgId+imgClass+imgTitle+imgAlt;
            strNewHTML += ‘style="position:relative;white-space:pre-line;display:inline-block;background:transparent;‘+imgAlign+imgHand;
            strNewHTML += ‘width:‘ + jQuery(this).width() + ‘px;‘ + ‘height:‘ + jQuery(this).height() + ‘px;‘;
            strNewHTML += ‘filter:progid:DXImageTransform.Microsoft.AlphaImageLoader‘ + ‘(src=\‘‘ + jQuery(this).attr(‘src‘) + ‘\‘, sizingMethod=\‘scale\‘);‘;
            strNewHTML += imgStyle+‘"></span>‘;
            if (prevStyle != ‘‘){
                strNewHTML = ‘<span style="position:relative;display:inline-block;‘+prevStyle+imgHand+‘width:‘ + jQuery(this).width() + ‘px;‘ + ‘height:‘ + jQuery(this).height() + ‘px;‘+‘">‘ + strNewHTML + ‘</span>‘;
            }

            jQuery(this).hide();
            jQuery(this).after(strNewHTML);
            //alert($(body).html());

        });

        // fix css background pngs
        jQuery(this).find("*").each(function(){
            var bgIMG = jQuery(this).css(‘background-image‘);
            if(bgIMG.indexOf(".png")!=-1){
                var iebg = bgIMG.split(‘url("‘)[1].split(‘")‘)[0];
                jQuery(this).css(‘background-image‘, ‘none‘);
                jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=‘" + iebg + "‘,sizingMethod=‘scale‘)";
            }
        });

        //fix input with png-source
        jQuery(this).find("input[src$=.png]").each(function() {
            var bgIMG = jQuery(this).attr(‘src‘);
            jQuery(this).get(0).runtimeStyle.filter = ‘progid:DXImageTransform.Microsoft.AlphaImageLoader‘ + ‘(src=\‘‘ + bgIMG + ‘\‘, sizingMethod=\‘scale\‘);‘;
           jQuery(this).attr(‘src‘, settings.blankgif)
        });

    }

    return jQuery;

};

})(jQuery);

<span style="position:relative;white-space:pre-line;display:inline-block;background:transparent;cursor:hand;width:378px;height:564px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=‘order_icon01.png‘, sizingMethod=‘scale‘);"></span>

原理:img图片隐藏,在img后面添加一个span标签,span的样式为img的样式,background:none;滤镜为图片的地址,position:relative;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src=‘jquery.js‘></script>

<style type="text/css">
.main{ background-color: red; min-height: 500px; height: auto !important; height: 500px; overflow: hidden;zoom:1;margin-bottom:100px;}
.btn_arw{ display:block;width:170px; height:39px;position:relative;}
.btn{ width:170px; height:39px;background:red;_background-image:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="order_icon02.png", sizingMethod="scale");}
a.btn_arw:hover{ color:red;}
</style>
</head>

<body>
<div class=‘main‘><a href=‘http://www.baidu.com1‘><img style=‘display:none;‘ src=‘order_icon01.png‘><span style="position:relative;white-space:pre-line;display:inline-block;background:transparent;cursor:hand;width:378px;height:564px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=‘order_icon01.png‘, sizingMethod=‘scale‘);"></span></a></div>
<div class=‘btn‘><a href=‘http://www.baidu.com2‘ class=‘btn_arw‘ style=‘width:170px;height:39px;‘>111222222</a></div>
</body>
</html>

【css】jquery.pngFix.js解决透明问题

时间: 2024-08-29 19:27:39

【css】jquery.pngFix.js解决透明问题的相关文章

.Net中使用response.write(&#39;js代码&#39;)后css失去效果,解决办法。

net中使用response.write输出js会将js放在源代码的最前面,这样就可能出现破坏网页css的效果,让css失去效果 .net中使用Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>if(confirm('确定要交卷吗?')==false){history.back()}; </script>"); 可以

用Jquery动态append方式加入标签时Css样式丢失的解决方法

一般在Jquery中会用下面的方式来添加新标签: var obj = "<fieldset data-role='controlgroup' data-type='vertical' data-role='fieldcontain'> <input id='menu0" type='checkbox'/><label for='menu0'> 复选框 </label> </fieldset>";  //按个人要求拼接

Jquery form.js文件上传返回JSON数据,在IE下提示下载文件的解决办法,并对返回数据进行正确的解析

Jquery from.js插件上传文件非常方便,但是在ie10以下的版本会弹出下载文件对话框 解决方法: 1.在服务端设置response.setContentType("text/plain"); 2.对返回数据正确解析                这时返回的数据不再是json,不能使用解析json的方法进行解析                       var data1=eval("(" + data+ ")");  //将返回的字符

PHP 弹窗 源代码 css Jquery.js

// 每个弹窗的标识 var x =0; var idzt = new Array(); var Window = function(config){ //ID不重复 idzt[x] = "zhuti"+x; //弹窗ID //初始化,接收参数 this.config = { width : config.width || 300, //宽度 height : config.height || 200, //高度 buttons : config.buttons || '', //默认

解决jquery.validate.js的验证bug

版本提示:jq为1.4.4, jquery.validate 为jQuery validation plug-in 1.7 问题: a.选填选项,如邮箱设置格式验证,那么情况输入框,验证label变成空白的[原来的默认提示没了] b.必填选项,如手机号码直接复制进去,格式是对的,验证label是不变的 1.母版页 $(function () { $.validator.setDefaults({ errorClass: "tip-error", errorPlacement: func

jquery.form.js不能解决连接超时(timeout)的解决方法

最近在使用jquery.form.js提交包含文件的表单时,碰到了一个问题:当碰上网速较慢时,而我们又设置了timeout时,例如: var options = { timeout: 3000 //限制请求的时间,当请求大于3秒后,跳出请求 } 我们的页面会死在这里,贴上F12开发者工具返回的结果: 此时,我们并没有处理错误的回调函数,而百度出来的例子中也只有这两个回调函数: beforeSubmit: showRequest, //提交前的回调函数 success: showResponse,

web项目js css静态文件缓存解决

在web开发过程中经常会遇到js及css文件缓存的问题,开发过程中我们可以自己手动的清空浏览器缓存,但是我们没法告诉用户去执行一个他们或许不熟悉的操作(清空浏览器缓存),为解决这个问题,编写了一个grunt插件,在前端构建的时候,可以使用grunt插件来做这件事情,就是将js及css文件自动的加上一个版本号(这里我用的是时间戳) 基于grunt的js 及css文件自动加version的插件,可以将配置的目录下所有引用到指定匹配的js及css文件加上一个版本号 github地址:    https

如何解决用jquery.uploadify.js进行多文档上传会修改document的title

在dwz框架中使用了jquery.uploadify.js进行多文档上传当打开窗口后会发现document的title被无意之中修改.进行代码调试也没有发现.对swfobject进行研究也没有发现修改title的情况. 只能采用一种方式禁止修改title var oldTitle = document.title; try { document.attachEvent('onpropertychange', function(){ if (document.title != oldTitle )

js文件最后加载(在window.load事件发生后再加载js文件),用于解决因jQuery等js库导致网页加载慢的问题

需引入文件:lazyload-min.js <script src="JS/lazyload-min.js" type="text/javascript"></script> 插入代码: function loadscript() { LazyLoad.loadOnce([ 'JS/touch.js', 'http://libs.baidu.com/jquery/1.2.3/jquery.min.js' ], loadComplete); }