Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamlined and unified opacity methods #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/prototype/dom/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,8 @@
*
**/
function getStyle(element, style) {
if (style === 'opacity') return getOpacity(element);

element = $(element);
style = normalizeStyleName(style);

Expand All @@ -2858,7 +2860,6 @@
value = css ? css[style] : null;
}

if (style === 'opacity') return value ? parseFloat(value) : 1.0;
return value === 'auto' ? null : value;
}

Expand All @@ -2883,6 +2884,8 @@
}

function getStyle_IE(element, style) {
if (style === 'opacity') return getOpacity_IE(element);

element = $(element);
style = normalizeStyleName_IE(style);

Expand All @@ -2893,9 +2896,6 @@
value = element.currentStyle[style];
}

if (style === 'opacity' && !STANDARD_CSS_OPACITY_SUPPORTED)
return getOpacity_IE(element);

if (value === 'auto') {
// If we need a dimension, return null for hidden elements, but return
// pixel values for visible elements.
Expand Down Expand Up @@ -2955,14 +2955,13 @@
// the standard approach (an `opacity` property in CSS) and the old-style
// IE approach (a proprietary `filter` property). They are written to
// prefer the standard approach unless it isn't supported.
function setOpacity_IE(element, value) {
// Prefer the standard CSS approach unless it's not supported.
if (STANDARD_CSS_OPACITY_SUPPORTED)
return setOpacity(element, value);
var setOpacity_IE = STANDARD_CSS_OPACITY_SUPPORTED ? setOpacity : function(element, value) {
element = $(element);
var style = element.style;
if (!element.currentStyle || !element.currentStyle.hasLayout)
style.zoom = 1;

element = hasLayout_IE($(element));
var filter = Element.getStyle(element, 'filter'),
style = element.style;
var filter = Element.getStyle(element, 'filter');

if (value == 1 || value === '') {
// Remove the `alpha` filter from IE's `filter` CSS property. If there
Expand All @@ -2980,7 +2979,7 @@
'alpha(opacity=' + (value * 100) + ')';

return element;
}
};


/**
Expand All @@ -2989,20 +2988,25 @@
* Returns the opacity of the element.
**/
function getOpacity(element) {
return Element.getStyle(element, 'opacity');
element = $(element);
// Try inline styles first.
var value = element.style.opacity;
if (!value || value === 'auto') {
// Reluctantly retrieve the computed style.
var css = document.defaultView.getComputedStyle(element, null);
value = css ? css.opacity : null;
}
return value ? parseFloat(value) : 1.0;
}

function getOpacity_IE(element) {
// Prefer the standard CSS approach unless it's not supported.
if (STANDARD_CSS_OPACITY_SUPPORTED)
return getOpacity(element);

// Prefer the standard CSS approach unless it's not supported.
var getOpacity_IE = STANDARD_CSS_OPACITY_SUPPORTED ? getOpacity : function(element) {
var filter = Element.getStyle(element, 'filter');
if (filter.length === 0) return 1.0;
var match = (filter || '').match(/alpha\(opacity=(.*)\)/);
if (match[1]) return parseFloat(match[1]) / 100;
return 1.0;
}
};


Object.extend(methods, {
Expand Down