1
我試圖做一個帶按鈕的自動幻燈片。我正在做間隔和點擊功能。帶按鈕的自動幻燈片
我正在使用此代碼,但它不起作用,並且DIV同時出現。有什麼問題?
這裏的HTML:
<div class="cajas" id="home-caja">
<div class="apartadosh">
<div class="diva">
<div class="espacioimagen">
<div class="fancyosop">
<p class="fancytitlep">News</p>
</div>
<div id="contentaboutp">
<p class="fancytextp">aaaaaaaaaaa</p>
</div>
<img src="../uploads/inphyxwords.png" class="fotohome" />
</div>
</div>
<div class="divb">
<div class="espacioimagen">
<div class="fancyosop">
<p class="fancytitlep">Web Online!</p>
</div>
<div id="contentaboutp" style="display:block">
<p class="fancytextp">bbbbbbbbbbbbb</p>
</div>
<img src="../uploads/41744f.png" class="fotohome" />
</div>
</div>
</div> <!-- Apartados -->
<div class="navh">
<ul>
<li><a href="#news" data-id="a">News</a></li>
<li><a href="#webonline" data-id="b">Web Online!</a></li>
</ul>
</div>
</div> <!-- Home caja -->
這裏Jquery的:
var pregunta = "a"; //Variable to check what DIV is visible
var escondido = $(".apartadosh > div:gt(0)");
$(escondido).hide(); //Hides the second DIV
var interval = undefined;
$(document).ready(function() {
interval = setInterval(getNext, 2000); //Interval function
$('.navh ul li a').on('click', getDiv); //When the button is clicked
});
function getDiv() {
var target = '.div' + $(this).attr("data-id");
if ($(this).attr("data-id") === pregunta) { //Check if you're clicking for the visible DIV
return 0}
else {
if (activa === "0") {
if ($(this).attr("data-id") === 'b') { //Set the variable to the current DIV ID
pregunta = "b"; }
if ($(this).attr("data-id") === 'a') {
pregunta = "a"; }
$(".apartadosh > div").fadeOut(1000);
$(target).fadeIn(1000);
}
}
}
function getNext() {
var $curr = $('.apartadosh div:visible'),
$next = ($curr.next().length) ? $curr.next() : $('.apartadosh div').first();
transition($curr, $next);
}
function transition($curr, $next) {
clearInterval(interval);
$curr.fadeOut(1000);
$next.fadeIn(1000);
});
}
首先,使用正確的語法 - 'VAR埃斯孔迪多=( 「.apartadosh> DIV:第n個孩子(2)」);'應該是' var escondido = $(「。apartadosh> div:nth-child(2)」);' – Firedrake969
仍然無效 – AtenrevCode