第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在多個(gè)圖像之間將最后一張圖像移動(dòng)到第一張的Javascript代碼

在多個(gè)圖像之間將最后一張圖像移動(dòng)到第一張的Javascript代碼

開滿天機(jī) 2023-04-14 17:24:04
<html><body>    <div>        <img src="cat.jpg" id="cat" onclick="alertfunction()">        <img src="dog.jpg" id="dog" onclick="alertfunction()">        <img src="frog.jpg" id="frog" onclick="movefunction()">        <img>    </div></body><script>    function alertfunction() {            alert("Don't click me, click the frog");}    function movefunction(){            }</script></html>在這里,我需要幫助,每當(dāng)用戶單擊最后一張圖片時(shí),移至第一張。請(qǐng)幫我。
查看完整描述

4 回答

?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊

您需要搜索的是 Javascript 中的 DOM 操作。這是一個(gè)簡(jiǎn)單的方法來(lái)做你所要求的。我試圖盡可能地解釋它。


<html>

<body>

    <div id="animals">

        <img src="cat.jpg" id="cat">

        <img src="dog.jpg" id="dog">

        <img src="frog.jpg" id="frog">

    </div>

</body>

<script>

    // animals variable here is a NodeList which is kind of like Array,

    // but it is not enumerable (you cannot loop over it).

    var animals = document.querySelectorAll('#animals > *');


    // Here we convert it to an actual Array.

    var animalsArray = Array.from(animals);


    // We are going to add click listeners on every single of them.

    animalsArray.forEach(function (animal) {


        // Add an event listener to this img element.

        animal.addEventListener(


            // The listener should observe click events.

            'click',


            // The listener will call this function

            // when a click event is triggered.

            function (event) {


                // This is the order number of the clicked animal image.

                // Assuming that you have 3 images on the page, this number

                // will be 0, 1 or 2.

                // 0 marking the image as the first of rest, and 2 marking it

                // as the last of them.

                var positionOfThisAnimal = animalsArray.indexOf(this);


                // animalsArray.length will give you the number of images in

                // the `animalsArray` Array.

                // If the position of the clicked animal image is smaller than

                // the highest it can be (which is `2` for 3 images), it is not

                // the last one, and we will alert the user with a message.

                if (positionOfThisAnimal < animalsArray.length - 1) {

                    alert("Don't click me! Click the frog.");


                    // Because all we have to do for these not-the-last-one

                    // images is to alert the user, we are returning a void

                    // (empty) result here which effectively stops the function

                    // from executing the rest of the lines we have below.

                    return;

                }


                // Here we are taking the clicked animal image (which is the

                // last one in the list) and we are moving it to the first

                // place in the div with ID "animals".

                document.querySelector('#animals').prepend(this);

            },


            // This `false` means the listener should not be marked as passive.

            //

            // Passive listeners are asynchronous, meaning that the browser

            // will *not* wait for your function to complete executing before

            // continuing to wait for other user inputs like mouse moves or

            // keyboard strokes.

            // 

            // The browser will wait for active listeners. This has a downside

            // though. If your function takes too long to complete, the user

            // will notice that their browser (or just the current tab) freezes

            // and won't let user interact with the page until the execution

            // is completed.

            false,

        );

    });

</script>

</html>


查看完整回答
反對(duì) 回復(fù) 2023-04-14
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊

您可以在用戶單擊圖像時(shí)更改圖像的來(lái)源,例如:


let cat = document.getElementById("cat");

let frog = document.getElementById("frog");

function moveFunction() {

  if(frog.src == "frog.jpg"){

    frog.src = "cat.jpg";

  }

  else {

    frog.src = "frog.jpg";

  }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-14
?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊

const img1 = document.querySelector('#cat');

                const img2 = document.querySelector('#dog');

                const img3 = document.querySelector('#frog');

                const imgUrl = img3.src;

                img1.addEventListener('click', () => {

                    if(img1.src === imgUrl) {

                        let temp = img1.src;

                        img1.src = img2.src;

                        img2.src = img3.src;

                        img3.src = temp;

                        let temp2 = img1.id;

                        img1.id = img2.id;

                        img2.id = img3.id;

                        img3.id = temp2;

                    }

                    else {

                        alert('Don\'t click me, click the frog');

                    };

                });

                img2.addEventListener('click', () => {

                    alert('Don\'t click me, click the frog');

                });

                img3.addEventListener('click', () => {

                    

                    if(img3.src === imgUrl) {

                        let temp = img1.src;

                        img1.src = img3.src;

                        img3.src = img2.src;

                        img2.src = temp;

                        let temp2 = img1.id;

                        img1.id = img3.id;

                        img3.id = img2.id;

                        img2.id = temp2;

                    }

                    else {

                        alert('Don\'t click me, click the frog');

                    };

                    

                });

img {

                width: 300px;

                height: 300px;

            }

<div>

            <img src="cat.jpg" id="cat">

            <img src="dog.jpg" id="dog">

            <img src="frog.jpg" id="frog">

        </div>


查看完整回答
反對(duì) 回復(fù) 2023-04-14
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

我們可以在 CSS 中使用 flex 和 order 來(lái)做到這一點(diǎn),檢查下面的代碼,而不是圖像,我使用了帶有顏色的 div。


.maindiv {

display: flex;

}


#frog {

padding: 10px;

width: 20px;

background: red;

}


#cat {

padding: 10px;

width: 20px;

background: blue;

}


#dog {

padding: 20px;

width: 10px;

background: green;

}

<html>

<body>

    <div class="maindiv">

        <div id="cat" onclick="alertfunction()">cat</div>

        <div id="dog" onclick="alertfunction()">dog</div>

        <div id="frog" onclick="movefunction()">frog</div>

    </div>

</body>

<script>

    function alertfunction() {

            alert("Don't click me, click the frog");

}

    function movefunction(){

    if (document.getElementById('frog').style.order == '1') {

        document.getElementById('frog').style.order = '3';

        document.getElementById('cat').style.order = '1';

        document.getElementById('dog').style.order = '2';

    } else {

        document.getElementById('frog').style.order = '1';

        document.getElementById('cat').style.order = '2';

        document.getElementById('dog').style.order = '3';

    }

        

    }



</script>

</html>


查看完整回答
反對(duì) 回復(fù) 2023-04-14
  • 4 回答
  • 0 關(guān)注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)