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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/prototype/dom/dom.js
Expand Up @@ -2847,6 +2847,8 @@
* *
**/ **/
function getStyle(element, style) { function getStyle(element, style) {
if (style === 'opacity') return getOpacity(element);

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


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


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


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


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

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


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


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

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


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


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


return element; return element;
} };




/** /**
Expand All @@ -2989,20 +2988,25 @@
* Returns the opacity of the element. * Returns the opacity of the element.
**/ **/
function getOpacity(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.
// Prefer the standard CSS approach unless it's not supported. var getOpacity_IE = STANDARD_CSS_OPACITY_SUPPORTED ? getOpacity : function(element) {
if (STANDARD_CSS_OPACITY_SUPPORTED)
return getOpacity(element);

var filter = Element.getStyle(element, 'filter'); var filter = Element.getStyle(element, 'filter');
if (filter.length === 0) return 1.0; if (filter.length === 0) return 1.0;
var match = (filter || '').match(/alpha\(opacity=(.*)\)/); var match = (filter || '').match(/alpha\(opacity=(.*)\)/);
if (match[1]) return parseFloat(match[1]) / 100; if (match[1]) return parseFloat(match[1]) / 100;
return 1.0; return 1.0;
} };




Object.extend(methods, { Object.extend(methods, {
Expand Down