Skip to content

Commit

Permalink
U: set sticky style before _supportSticky fn
Browse files Browse the repository at this point in the history
  • Loading branch information
lizzie committed Jun 26, 2013
1 parent beb015c commit e2233e7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
24 changes: 9 additions & 15 deletions dist/sticky-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ define("arale/sticky/1.2.1/sticky-debug", [ "$-debug" ], function(require, expor
// sticky.fix 无法用 sticky 方式来实现
if (sticky.isPositionStickySupported && !callFix) {
scrollFn = this._supportSticky;
// 直接设置 sticky 的样式属性
var tmp = "";
for (var i = 0; i < stickyPrefix.length; i++) {
tmp += "position:" + stickyPrefix[i] + "sticky;";
}
this.elem[0].style.cssText += tmp + "top: " + this.marginTop + "px;";
} else if (sticky.isPositionFixedSupported) {
scrollFn = this._supportFixed;
} else {
Expand Down Expand Up @@ -105,12 +111,6 @@ define("arale/sticky/1.2.1/sticky-debug", [ "$-debug" ], function(require, expor
}
};
Sticky.prototype._supportSticky = function() {
// 直接设置 sticky 的样式属性
var tmp = "";
for (var i = 0; i < stickyPrefix.length; i++) {
tmp += "position:" + stickyPrefix[i] + "sticky;";
}
this.elem[0].style.cssText += tmp + "top: " + this.marginTop + "px;";
// 由于 position:sticky 尚未提供接口判断状态
// 因此仍然要计算 distance 以便进行回调
var distance = this._originTop - doc.scrollTop();
Expand All @@ -129,10 +129,11 @@ define("arale/sticky/1.2.1/sticky-debug", [ "$-debug" ], function(require, expor
this.elem.data("sticked", false);
this.callback.call(this, false);
};
// 需要占位符的情况有: 1) position: static or relative; 但除了 display 不是 block
// 需要占位符的情况有: 1) position: static or relative,除了 display 不是 block 的情况
Sticky.prototype._addPlaceholder = function() {
var need = false;
if (indexOf([ "static", "relative" ], this.elem.css("position")) !== -1) {
var position = this.elem.css("position");
if (position === "static" || position === "relative") {
need = true;
}
if (this.elem.css("display") !== "block") {
Expand Down Expand Up @@ -200,11 +201,4 @@ define("arale/sticky/1.2.1/sticky-debug", [ "$-debug" ], function(require, expor
return isSupported;
}
}
function indexOf(array, item) {
if (array == null) return -1;
var nativeIndexOf = Array.prototype.indexOf;
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
for (var i = 0; i < array.length; i++) if (array[i] === item) return i;
return -1;
}
});
2 changes: 1 addition & 1 deletion dist/sticky.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ define(function (require, exports, module) {
var scrollFn;
// sticky.fix 无法用 sticky 方式来实现
if (sticky.isPositionStickySupported && !callFix) {
scrollFn = this._supportSticky;
scrollFn = this._supportSticky;

// 直接设置 sticky 的样式属性
var tmp = "";
for (var i = 0; i < stickyPrefix.length; i++) {
tmp += "position:" + stickyPrefix[i] + "sticky;";
}
this.elem[0].style.cssText += tmp + "top: " + this.marginTop + "px;";


} else if (sticky.isPositionFixedSupported) {
scrollFn = this._supportFixed;
} else {
Expand Down Expand Up @@ -132,13 +141,6 @@ define(function (require, exports, module) {
};

Sticky.prototype._supportSticky = function () {
// 直接设置 sticky 的样式属性
var tmp = "";
for (var i = 0; i < stickyPrefix.length; i++) {
tmp += "position:" + stickyPrefix[i] + "sticky;";
}
this.elem[0].style.cssText += tmp + "top: " + this.marginTop + "px;";

// 由于 position:sticky 尚未提供接口判断状态
// 因此仍然要计算 distance 以便进行回调
var distance = this._originTop - doc.scrollTop();
Expand Down
2 changes: 1 addition & 1 deletion tests/sticky-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ define(function (require) {
$(document).scrollTop(elementTop + 300);

setTimeout(function () {
expect(element.css('position')).to.be("static");
expect(element.css('position').indexOf("sticky") !== -1 || element.css('position') === "static").to.be(true);
done();
element.show();
obj.destory();
Expand Down

0 comments on commit e2233e7

Please sign in to comment.