Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

update the method offset() #732

Open
wants to merge 1 commit into from

3 participants

@clareSun

getting the element's attribute(such as: height) which has the style "display:none".

@clareSun clareSun Update zepto.js
getting the element's attribute(such as: height) which has the style "display:none".
5c30ad4
@madrobby
Owner

This would be good-to-have for jQuery compatibility, but is missing tests.

@sgruhier

why not just call obj = elem.getBoundingClientRect() ?

@sgruhier

Do you really need this method? It's called only once

@madrobby madrobby was assigned
@madrobby
Owner

Any takers for a better implementation of this, that includes tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2013
  1. @clareSun

    Update zepto.js

    clareSun authored
    getting the element's attribute(such as: height) which has the style "display:none".
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 3 deletions.
  1. +29 −3 src/zepto.js
View
32 src/zepto.js
@@ -32,8 +32,10 @@ var Zepto = (function() {
toString = class2type.toString,
zepto = {},
camelize, uniq,
- tempParent = document.createElement('div')
-
+ tempParent = document.createElement('div'),
+ //detection object style
+ cssShow = { position:"absolute", visibility:"hidden", display:"block" }
+
zepto.matches = function(element, selector) {
if (!element || element.nodeType !== 1) return false
var matchesSelector = element.webkitMatchesSelector || element.mozMatchesSelector ||
@@ -325,7 +327,19 @@ var Zepto = (function() {
$.grep = function(elements, callback){
return filter.call(elements, callback)
}
+ //swap object
+ $.swap = function (elem, options, callback) {
+ var old = {};
+ for (var name in options) {
+ old[ name ] = elem.style[ name ];
+ elem.style[ name ] = options[ name ];
+ }
+ callback.call(elem);
+ for (name in options) {
+ elem.style[ name ] = old[ name ];
+ }
+ }
if (window.JSON) $.parseJSON = JSON.parse
// Populate the class2type map
@@ -600,7 +614,15 @@ var Zepto = (function() {
$this.css(props)
})
if (this.length==0) return null
- var obj = this[0].getBoundingClientRect()
+ //determine whether is a hiden element
+ var obj = this[0].getBoundingClientRect(),elem=this[0]
+ if (elem.offsetWidth !== 0) {
+ obj = elem.getBoundingClientRect()
+ } else {
+ $.swap(elem, cssShow, function () {
+ obj = getHiddenClientRect(elem);
+ });
+ }
return {
left: obj.left + window.pageXOffset,
top: obj.top + window.pageYOffset,
@@ -722,6 +744,10 @@ var Zepto = (function() {
})
}
})
+
+ function getHiddenClientRect(elem) {
+ return elem.getBoundingClientRect();
+ }
function traverseNode(node, fun) {
fun(node)
Something went wrong with that request. Please try again.