클래스가 뷰에 있는지 확인하고 클래스를 추가하는 경우 해당 클래스가 작동하는지 확인하는 코드가 있습니다. 보기에 div가있는 경우에만 클래스 상자 활성을 추가하려고합니다.Jquery 요소가 뷰에있을 때 클래스 추가 중
지금 당분간 지 냈습니다. 코드에 무엇이 잘못되었는지 말해 줄 수 있습니까? 가능한 수정 또는 어떻게 수정해야하는지 알려줍니다.
codepen : http://codepen.io/salman15/pen/rLRZrJ
jQuery를
$(document).ready(function() {
$('#next').click(function() {
if ($('.in1,.in2,.in3').next('.t1,.t2,.t3').length) {
$('.t1').animate({
left: '-1000px'
})
$('.in1').removeClass('in1')
.next('.t1')
.addClass('in1');
$('.t2').animate({
right: '-1000px'
})
$('.in2').removeClass('in2')
.next('.t2')
.addClass('in2');
$('.t3').animate({
bottom: '-1000px'
})
$('.in3').removeClass('in3')
.next('.t3')
.addClass('in3');
}
});
$('#prev').click(function() {
if ($('.in1,.in2,.in3').prev('.t1,.t2,.t3').length) {
$('.t1').animate({
left: '-1000px'
})
$('.in1').removeClass('in1')
.prev('.t1')
.addClass('in1');
$('.t2').animate({
right: '-1000px'
})
$('.in2').removeClass('in2')
.prev('.t2')
.addClass('in2');
$('.t3').animate({
bottom: '-1000px'
})
$('.in3').removeClass('in3')
.prev('.t3')
.addClass('in3');
}
});
});
$.fn.isVisible = function() {
// Am I visible?
// Height and Width are not explicitly necessary in visibility detection, the bottom, right, top and left are the
// essential checks. If an image is 0x0, it is technically not visible, so it should not be marked as such.
// That is why either width or height have to be > 0.
var rect = this[0].getBoundingClientRect();
return (
(rect.height > 0 || rect.width > 0) &&
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
);
};
if ($('.box').isVisible()) {
setTimeout(function(){
$('.box').removeClass('box-active')}, 4000);
} else{
setTimeout(function(){
$('.box').addClass('box-active')}, 4000);
};
왜 당신이 당신의 전체 프로젝트를 추가 한? : P 당신은 단지 관련 코드를 추가 할 수 있습니까 –
때로는 HTML에 대한 요청을받습니다 : D – Salman
jquery는 계속 엉망입니다. 당신은 하나의 클릭 기능 .. if 문장 만 필요합니다. 당신이 가지고있는 방식대로, 당신이 기대하는대로 일이 일어나는 것이 놀랍습니다. – Scott