私は3つのスライドのアコーディオンメニューを持っています。マウスカーソルがメニューdivを離れるときにそのスライドを開いたままにするために、チェック状態のためにそれを使用できるように、ホバー上で最後のアクティブなクラスを取得する必要があります。今まで私は各スライドにクラスを追加することができましたが、メニューdiv内にあるかどうかだけを確認します。最後にアクティブなスライドを確認する方法はありますか?
Codepenマウスカーソルがdivを離れるときに、ホバー上で最後にアクティブなクラスを取得しますか?
HTMLコード
<div class="myMenu">
<ul>
<li class="mySlides slide1"><a class="img1" href="#"></a></li>
<li class="mySlides slide2"><a class="img2" href="#"></a></li>
<li class="mySlides slide3"><a class="img3" href="#"></a></li>
</ul>
</div>
はJavaScript
var slide1 = document.querySelector('.slide1');
var slide2 = document.querySelector('.slide2');
var slide3 = document.querySelector('.slide3');
var img1 = document.querySelector('.img1');
var img2 = document.querySelector('.img2');
var img3 = document.querySelector('.img3');
/* */
document.querySelector('.myMenu').addEventListener('mouseout', mouseOutMenu);
function mouseOutMenu() {
if(slide1.classList.contains('insideMenu')) {
slide1.style.width = '298px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_open_leszbs.jpg')";
slide2.style.width = '21px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_closed_aonaaq.jpg')";
slide3.style.width = '21px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_closed_tobbqu.jpg')";
}
if(slide2.classList.contains('insideMenu')) {
slide2.style.width = '301px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_open_g8qfff.jpg')";
slide1.style.width = '21px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_closed_pnlynb.jpg')";
slide3.style.width = '21px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_closed_tobbqu.jpg')";
}
if(slide3.classList.contains('insideMenu')) {
slide3.style.width = '301px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_open_gcengd.jpg')";
slide1.style.width = '21px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_closed_pnlynb.jpg')";
slide2.style.width = '21px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_closed_aonaaq.jpg')";
}
}
/**************** 1 *******************/
/**********************************************************/
document.querySelector('.slide1').addEventListener('mouseover', mouseOver1);
function mouseOver1() {
slide1.classList.add('insideMenu');
if(slide1.style.width == '298px') {
slide1.style.width = '298px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_open_leszbs.jpg')";
} else {
slide1.style.width = '298px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_open_leszbs.jpg')";
}
slide2.style.width = '21px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_closed_aonaaq.jpg')";
slide3.style.width = '21px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_closed_tobbqu.jpg')";
}
document.querySelector('.slide1').addEventListener('mouseout', mouseOut1);
function mouseOut1() {
if(slide1.style.width > '21px') {
slide1.style.width = '21px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_closed_pnlynb.jpg')";
}
}
/**************** 2 *******************/
/**********************************************************/
document.querySelector('.slide2').addEventListener('mouseover', mouseOver2);
function mouseOver2() {
slide2.classList.add('insideMenu');
if(slide2.style.width == '301px') {
slide2.style.width = '301px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_open_g8qfff.jpg')";
} else {
slide2.style.width = '301px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_open_g8qfff.jpg')";
}
// close the rest of slides
slide1.style.width = '21px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_closed_pnlynb.jpg')";
slide3.style.width = '21px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_closed_tobbqu.jpg')";
}
document.querySelector('.slide2').addEventListener('mouseout', mouseOut2);
function mouseOut2() {
if(slide2.style.width > '21px') {
slide2.style.width = '21px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_closed_aonaaq.jpg')";
}
}
/**************** 3 *******************/
/**********************************************************/
document.querySelector('.slide3').addEventListener('mouseover', mouseOver3);
function mouseOver3() {
slide3.classList.add('insideMenu');
if(slide3.style.width == '301px') {
slide3.style.width = '301px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_open_gcengd.jpg')";
} else {
slide3.style.width = '301px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_open_gcengd.jpg')";
}
slide2.style.width = '21px';
img2.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/2_closed_aonaaq.jpg')";
slide1.style.width = '21px';
img1.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/1_closed_pnlynb.jpg')";
}
document.querySelector('.slide3').addEventListener('mouseout', mouseOut3);
function mouseOut3() {
if(slide3.style.width > '21px') {
slide3.style.width = '21px';
img3.style.backgroundImage = "url('https://res.cloudinary.com/dijc9b7wz/image/upload/v1511157235/3_closed_tobbqu.jpg')";
}
}
/**********************************************************/
はそうあなたがマウスをスライドまたはも上にあるとき、スライドループを停止したい: 最後に、小さいこのセクションの方法を作ったメニューのdiv要素のメインマウスアウトイベントにそれらを追加しましたマウスがスライドをつかんで出てきたのですか?この最後のオプションでは、カルーセルはもう自動的にはスライドしません。 – Giox
はい。ループが終了すると、マウスがメニューdivをホバーします。 マウスカーソルがdivを離れると、10秒後に再度ループすることはできますか? – holler