gpt4 book ai didi

jquery globalcss IE 不透明度

转载 作者:行者123 更新时间:2023-11-28 11:51:16 24 4
gpt4 key购买 nike

我正在使用 jquery globalcss 插件来更改全局样式表。它不处理不透明度和 IE。

我一直在尝试让它正常工作,但运气不佳。这是我强制插件尝试理解 IE 样式不透明度的尝试。

function changeCss (property, value, target) {
if (property === "opacity") {
$(target).globalcss("filter","alpha(opacity="+value*100+")");
/* For IE 8 (and 9, 10, 11?). Don't miss the added quotes */
$(target).globalcss("-MS-filter","\"progid:DXImageTransform.Microsoft.Alpha(Opacity="+value*100+")\"");

}
$(target).globalcss(property,value);
}

废话。如果有人可以提供帮助,那就太好了。谢谢。

我将插件粘贴在这里,因为它不再位于其原始站点上:

/*
* Global Stylesheet jQuery Plugin
* Version: 0.1
*
* Enables CSS modification that uses a 'global' stylesheet, rather than inline CSS.
*
* Copyright (c) 2009 Jeremy Shipman (http://www.burnbright.co.nz)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* INSTRUCTIONS:
* use in the same way as the jQuery css function. Eg:
* $("some selector").globalcss("style","value");
*
* use the globalsetylesheet.print() function to return a string of the global stylesheet
*/
(function($) {

//global singleton class for
globalstylesheet = new function globalStylesheet(){
if(!document.styleSheets){
alert("document.Stylesheets not found");
return false;
}

var sheet = null;
var setrules = Array(); // rule cache

//set up a dummy noded
var cssNode = document.createElement('style');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.media = 'screen';
cssNode.title = 'globalStyleSheet';
document.getElementsByTagName("head")[0].appendChild(cssNode);

//find the newly created stylesheet and store reference
for(var i = 0; i < document.styleSheets.length; i++){
if(document.styleSheets[i].title == "globalStyleSheet"){
sheet = document.styleSheets[i];
}
}

//set a CSS rule
this.setRule = function setRule(selector,ruleText){
if(setrules[selector] != undefined){
return setrules[selector];
}else{
if(sheet.addRule){ // IE
sheet.addRule(selector,ruleText,0);
}else{
sheet.insertRule(selector+'{'+ruleText+'}',0);
}
setrules[selector] = this.getRule(selector);
}
return setrules[selector];
}

//get a saved CSS rule
this.getRule = function getRule(selector){
if(setrules[selector] != undefined){
return setrules[selector];
}else{
var rules = allRules();
for(var i = 0; i < rules.length; i++){
if(rules[i].selectorText == selector){
return rules[i];
}
}
}
return false;
}

//helper function to get all rules
function allRules(){
if(sheet.cssRules){ //IE
return sheet.cssRules;
}else{
return sheet.rules;
}
}

//print out the stylesheet
this.print = function print(){
var styleinfo = "";
var rules = allRules();
for(var i = 0; i < rules.length; i++){
styleinfo+= rules[i].cssText+"\n"
}
return styleinfo;
}

//use jQuery's css selector function to set the style object
this.css = function css(jquery,key,value){
rule = this.setRule(jquery.selector,key+":"+value+";");
jQuery(rule).css(key,value);
}
}

//hook new function into jQuery
jQuery.fn.extend({
globalcss : function globalcss(key,value){
globalstylesheet.css(this,key,value);
}
});

})(jQuery);

编辑:我创建了一个 jsbin 现场演示。请在不同的浏览器中进行比较。 http://jsbin.com/iqadu/edit

最佳答案

更新:

我下面的第一个想法不是问题。事实证明,这个问题要简单得多:确保在小于 1 的 float 前加一个零。也许这是由于 javascript 如何处理字符串值到数字的转换?无论如何,在“.5”之前添加一个零解决了这个问题。

    // works
var property = "opacity";
var value = "0.5";
var target = ".transparency";

// doesn't work
var property = "opacity";
var value = ".5";
var target = ".transparency";

在这里查看工作代码:http://jsbin.com/ikore3 .顺便说一句,您原来的演示页面有一个 javascript 问题,大括号放在错误的位置。我也解决了这个问题。

以下是导致此问题的最初想法 - 结果证明不是问题

您可能会遇到与 jquery 无关的 IE 怪癖:为了使不透明度起作用,元素必须具有“layout”,这是一个 IE 特定的 bool 状态,由 CSS 触发,例如高度、宽度、缩放等。如果元素没有“布局”,则不透明度将不起作用。

解决方法是向元素添加特定的 CSS,以便为其提供“布局”。不确定这是否是您的情况,但通过添加提供布局的 CSS 属性之一并查看问题是否消失,很容易进行检查。

来自 http://joseph.randomnetworks.com/archives/2006/08/16/css-opacity-in-internet-explorer-ie/ :

But simply setting the filter/opacity value in IE isn’t enough. Turns out that IE requires an element to be positioned in order for filter/opacity to work. If your element doesn’t have a position you can work around this by adding ‘zoom: 1‘ to your CSS. There are other hacks to deal with this, zoom was the one I picked and it seems to work well enough.

In JavaScript you can find out if an element in IE has a position component by checking element.currentStyle.hasLayout. If hasLayout is false then the element has no CSS positioning.

If you want to learn more about this issue here is a reading list to get your started:

◦ Exploring OpacityStep-by-Step [broken link]

On having layout

Opacity @ QuirksMode

使用您的代码,这是一种可能的方法来确保如何测试这是否是您的问题:

function changeCss (property, value, target) {
if (property === "opacity") {
$(target).globalcss("filter","alpha(opacity="+value*100+")");
/* For IE 8 (and 9, 10, 11?). Don't miss the added quotes */
$(target).globalcss("-MS-filter","\"progid:DXImageTransform.Microsoft.Alpha(Opacity="+value*100+")\"");

$(target).globalcss("zoom","1"); // ensure the target has layout
}
$(target).globalcss(property,value);
}

更复杂的版本会检查缩放是否已经存在,只有在缺失时才添加它。您还可以检查 hasLayout property 并且只有在它为 false 时才设置缩放,只要你防御 hasLayout 根本不存在的非 IE 情况。

如果这不能解决问题,您能否发布 HTML 示例或 URL,以便我们重现问题,从而更容易提出修复建议?谢谢!

关于jquery globalcss IE 不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1665313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com