我试图在点击' .sbSelector'时将窗口向下滚动200px,如果点击的元素(即' .sbSelector')位于小于距离窗户高度200px。
$(document).on('click', '.sbSelector', function(){
var windowHeight = $(window).height();
var drpdwnOffsetTop = parseFloat($(this).offset().top - $(window).scrollTop());
if(parseFloat(windowHeight - drpdwnOffsetTop) < 200){
var temp = $(window).scrollTop() + 200;
$('html, body').animate({scrollTop: temp}, 500, function(){
**// How to get the instance of current '.sbSelector' here**
**// $(this) ll point to $(html,body), where as i need a reference to the current $('.sbSelector')**
});
}
});
我的查询是如何获取当前&#39; .sbSelector&#39;的实例。在$(&#39; html,body&#39;)。animate函数回调中。 Bcoz可能有很多&#39; .sbSelector&#39;文档中的元素,我想指向当前点击的&#39; .sbSelector&#39;在动画回调中。
答案 0 :(得分:2)
有关全局变量的更多信息,请查看JavaScript Scope。
SELECT @age = age,
@name = name,
@address = address
FROM dbo.test
WHERE primarykey = somevalue
或者,您可以将其绑定到Browser Object Model:
$(document).on('click', '.sbSelector', function(){
var windowHeight = $(window).height();
var drpdwnOffsetTop = parseFloat($(this).offset().top - $(window).scrollTop());
var that = $(this);
if(parseFloat(windowHeight - drpdwnOffsetTop) < 200){
var temp = $(window).scrollTop() + 200;
$('html, body').animate({scrollTop: temp}, 500, function(){
console.log(that);
});
}
});
注意:在窗口对象上显式设置时,请注意不要覆盖窗口对象的当前属性。此外,这在$(document).on('click', '.sbSelector', function(){
var windowHeight = $(window).height();
var drpdwnOffsetTop = parseFloat($(this).offset().top - $(window).scrollTop());
window.that = $(this);
if(parseFloat(windowHeight - drpdwnOffsetTop) < 200){
var temp = $(window).scrollTop() + 200;
$('html, body').animate({scrollTop: temp}, 500, function(){
console.log(window.that);
});
}
});
等特定环境中无效。