简体   繁体   中英

Need help adding functions to a button in html

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .in-middle {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .button {
            border-radius: 50%;
            background-color: red;
            color: white;
        }
    </style>
</head>

<body>
    <div class="in-middle">
        <button class="button">
            click me
        </button>
    </div>
</body>

</html>

Basically, i have this code for a button. How do i add a function that starts playing music with the controller hidden, and sends you to next page while still playing the music?

you have to audio source first then assign a function on the button

<audio id="myAudio">
  <source src="horse.ogg" type="audio/ogg">
</audio> 
<div id="controler">

 </div>
<div class="in-middle">
            <button class="button" onclick="myfunction">
                click me
            </button>
  </div>

then add the function on the js file

function myfunction(){
    document.getElementById("myAudio").play(); //it  will start the audio
    document.getElementById("controler").style.display="none" // it will hide your controller
    window.location.href="pageurl"; // this will open new window
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM