如何使它從頂部偏移50%?我也嘗試添加- ($window.height()/2)
偏移().top - 頂部50%?
我可以設置像素距離$(this).offset().top - 800)
但我想使用百分比代替。 50%只是任意的,也可能是25%,等等。
以下是完整的腳本,如果想知道:
// constants
var BTN_CLS = 'owl-thumb-item',
\t \t BTN_ANIMATION_MILLIS = 200,
\t \t DIV_ANIMATION_MILLIS = 1000;
// document ready handler
$(document).ready(function() {
\t
// display buttons from first 'div'
showBtns('one', BTN_CLS);
// window scroll handler
$(window).scroll(function() {
$('.hidden').each(function(i, v) {
if ($(window).scrollTop() > $(this).offset().top - 800) {
\t // show 'div' when scrolling
\t \t
\t
\t \t
\t showDiv($(this), onCompleteDivAnimation($(this)));
}
});
});
});
/**
* Used to show an animated 'div' and perform some actions.
* @param {Function} completeCallback Action performed after animation.
* @param {Object} div Target element.
*/
function showDiv(div, completeCallback) {
// check if 'div' is currently animated and avoid animation queue
if (!div.is(':animated')) {
div.animate({
opacity: 1
}, {
complete: completeCallback,
duration: DIV_ANIMATION_MILLIS
});
}
};
/**
* Used to perform actions after completing a 'div' animation.
*/
function onCompleteDivAnimation(div) {
\t showBtns(div.prop('id'), BTN_CLS);
};
/**
* Used to show button(s) from a 'div' element.
* @param {String} divId Target element Id.
* @param {String} btnCls Button(s) CSS class.
*/
function showBtns(divId, btnCls) {
var btnGroup = getBtnGroup(divId, btnCls);
animateBtn(btnGroup);
};
/**
* Used for creating a group of button(s) from a 'div' element.
* @param {String} divId Target element Id.
* @param {String} btnCls Button(s) CSS class.
* @returns {Array} btnGroup
*/
function getBtnGroup(divId, btnCls) {
var domBtns = $('#' + divId + ' .' + btnCls),
btnGroup = [];
for (var i = 0; i < (domBtns || []).length; ++i) {
btnGroup.push(domBtns[i]);
}
return btnGroup;
};
/**
* Used to animate a button group that normally comes from a 'div' element.
*/
function animateBtn(btnGroup) {
\t btnGroup = btnGroup || [];
$(btnGroup.shift()).fadeIn(BTN_ANIMATION_MILLIS, function() {
if (btnGroup.length > 0) {
animateBtn(btnGroup);
}
});
};
當你說從頂部偏移50%,你只是想垂直居中呢? – Pabs123
用例的正確解釋會有所幫助。你真的不清楚你的目標是什麼 – charlietfl
你有沒有試圖在你的if語句之前把它放入var中? 'var offElem = $(this).offset()。top; var offset50 = offElem - (($ window.height())/ 2); *** INSERT IF STATEMENT HERE ***' – Evochrome