通过在函数构造中传递 $ 运算符,我认为我应该可以清楚地在代码中使用 $ 。但是,它会抛出“对象不是函数”错误。你知道我做错了什么吗?
完整代码如下:
<script>
function isElementVisible($elementToBeChecked)
{
var TopView = $(window).scrollTop();
var BotView = TopView + $(window).height();
var TopElement = $elementToBeChecked.offset().top;
var BotElement = TopElement + $elementToBeChecked.height();
return ((BotElement <= BotView) && (TopElement >= TopView));
}
jQuery(window).scroll(function($) {
$( ".counter" ).each(function() {
$this = $(this);
isOnView = isElementVisible($(this));
if(isOnView && !$(this).hasClass('Starting')){
$(this).addClass('Starting');
startTimer($(this));
}
});
});
function startTimer($this) {
setTimeout(function(){
$this.html($this.html() - 1);
startTimer($this);
}, 1000);
}
</script>
最佳答案
您在多个地方使用了美元符号,如果存在冲突,为什么还要在那里定义它?
您应该做的是将所有代码包装在推荐的 WordPress DOM 就绪处理程序中
<script>
jQuery(function($) {
function isElementVisible($elementToBeChecked) {
var TopView = $(window).scrollTop();
var BotView = TopView + $(window).height();
var TopElement = $elementToBeChecked.offset().top;
var BotElement = TopElement + $elementToBeChecked.height();
return ((BotElement <= BotView) && (TopElement >= TopView));
}
$(window).scroll(function() {
$( ".counter" ).each(function() {
$this = $(this);
isOnView = isElementVisible($(this));
if(isOnView && !$(this).hasClass('Starting')){
$(this).addClass('Starting');
startTimer($(this));
}
});
});
function startTimer($this) {
setTimeout(function(){
$this.html($this.html() - 1);
startTimer($this);
}, 1000);
}
});
</script>
关于jQuery Uncaught TypeError : object is not a function. WordPress $ 对象冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25106392/