如何使图像更改更快?

时间:2019-04-10 23:03:28

标签: javascript html css image

我的代码工作得很好,但是由于某些原因,当您单击播放按钮时,切换到暂停符号会花费更长的时间,偶尔,暂停按钮也需要更长的时间才能转到播放按钮。我还有一个问题,当您单击“雨和海滩”图标时,会有一个蓝色方形边框,我不记得放了,如何删除它? 谢谢,Love2code

<!DOCTYPE html>
<html>
<head>
    <title>Meditation App</title>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <style>
        *{
            margin:0;
            padding:0;
            box-sizing:border-box;

        }
        .app{
            height:100vh;
            display:flex;
            justify-content:space-evenly;
            align-items:center;

        }
        .time-select,.sound-picker,.player-container{
            height:80%;
            flex:1;
            display:flex;
            flex-direction:column;
            justify-content:space-evenly;
            align-items:center;

        }
        .player-container{
            position:relative;
        }
        .player-container svg{
            position:absolute;
            height:50%;
            top:50%;
            left:50%;
            transform:translate(-50%,-50%);
            pointer-events:none;

        }

        .time-display{
            position:absolute;
            bottom:10%;
            color:white;
            font-size:50px;
        }
        video{
            position:fixed;
            top:0%;
            left:0%;
            width:100%;
            z-index:-10;

        }
        .time-select button, 
        .sound-picker button{
            color:white;
            width:30%;
            height:10%;
            background:none;
            border:2px solid white;
            cursor:pointer;
            border-radius:5px;
            font-size:20px;
            transition:all 0.5s ease;
        }
        .time-select button:hover{
            color:black;
            background:white;

        }
        .sound-picker button{
            border:none;
            height:120px;
            width:120px;
            border-radius:50%;
        }
        .sound-picker button:nth-child(1){
            background:#4972a1;
			

        }
        .sound-picker button:nth-child(2){
            background:#a14f49;
        }
		.sound-picker:focus{
  outline: none;
}

    </style>
</head>
<body>
    <div class="app">
        <div class="vid-container">
            <video Loop>
                <source src="https://www.dropbox.com/s/wkdu9elom9o4r5g/rain%20%281%29.mp4?raw=1"/>
            </video>
        </div>
        <div class="time-select">
            <button data-time="120">2 Minutes</button>
            <button data-time="300">5 Minutes</button>
            <button data-time="600">10 Minutes</button>
        </div>
        <div class="player-container">
            <audio class="song">
                <source src="https://www.dropbox.com/s/jawlfpyyz83w2td/rain.mp3?raw=1"></source>
            </audio>
        	<img src="https://www.dropbox.com/s/8unx3knosmefedk/download%20%281%29.svg?raw=1" class="play-container" alt="">
        <svg class="track-outline"
        width="453" height="453" viewBox="0 0 453 453" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="226.5" cy="226.5" r="216.5" stroke="white" stroke-width="20"/>
    </svg>
    <svg class="moving-outline"
    width="453" height="453" viewBox="0 0 453 453" fill="none" xmlns="http://www.w3.org/2000/svg">
    <circle cx="226.5" cy="226.5" r="216.5" stroke="#018EBA" stroke-width="20"/>
</svg>
<h3 class="time-display">0:00</h3>
</div>
<div class="sound-picker">
    <button data-sound="https://www.dropbox.com/s/jawlfpyyz83w2td/rain.mp3?raw=1" data-video="https://www.dropbox.com/s/wkdu9elom9o4r5g/rain%20%281%29.mp4?raw=1"><img src="https://i.ibb.co/8BspYTV/rain-1.png"></button>
    <button data-sound="https://www.dropbox.com/s/6k9nauf2ffyvfuu/beach.mp3?raw=1"
    data-video="https://www.dropbox.com/s/tsdd86bxmax32jp/beach.mp4?raw=1"><img src="https://i.ibb.co/T0xw4k7/sun-umbrella.png"></button>


</div>





</body>
<script>
  const app = () => {
        const song = document.querySelector(".song");
        const play = document.querySelector(".play-container");
        const outline = document.querySelector(".moving-outline circle");
        const video = document.querySelector(".vid-container video");

        //Sounds
        const sounds = document.querySelectorAll(".sound-picker button");
        //Time Display
        const timeDisplay = document.querySelector(".time-display");
        //Get length of the outside
        const outlineLength = outline.getTotalLength();
        //Duration
        let fakeDuration = 600;

        outline.style.strokeDasharray = outlineLength;
		outline.style.strokeDashoffset = outlineLength;
		
		//play sounds
		play.addEventListener("click", () => {
			checkPlaying(song);
		});
		//stop and play the sounds
		const checkPlaying = song =>{
				if(song.paused){
			song.play();
			video.play();
			play.src = 'https://www.dropbox.com/s/3zvnjkebwt1sjgq/download%20%283%29.svg?raw=1';
		}else{
			song.pause();
			video.pause();
			play.src = 'https://www.dropbox.com/s/8unx3knosmefedk/download%20%281%29.svg?raw=1';
		}
		}
    };  
    app();
</script>
</html>

1 个答案:

答案 0 :(得分:3)

由于您的SVG是远程来源的,因此下载它们会花费一些时间。我用远程源创建了两个隐藏的图像。这将下载这些文件,并在需要时用您的现金准备好它们。我还更改了在播放视频之前切换源路径的顺序。 我将所有元素设置为没有轮廓,以在单击图像时消除蓝色边框。

const app = () => {
  const song = document.querySelector(".song");
  const play = document.querySelector(".play-container");
  const outline = document.querySelector(".moving-outline circle");
  const video = document.querySelector(".vid-container video");

  //Sounds
  const sounds = document.querySelectorAll(".sound-picker button");
  //Time Display
  const timeDisplay = document.querySelector(".time-display");
  //Get length of the outside
  const outlineLength = outline.getTotalLength();
  //Duration
  let fakeDuration = 600;

  outline.style.strokeDasharray = outlineLength;
  outline.style.strokeDashoffset = outlineLength;

  //play sounds
  play.addEventListener("click", () => {
    checkPlaying(song);
  });
  //stop and play the sounds
  const checkPlaying = song => {
    if (song.paused) {
      play.src = 'https://www.dropbox.com/s/3zvnjkebwt1sjgq/download%20%283%29.svg?raw=1';
      song.play();
      video.play();

    } else {
      play.src = 'https://www.dropbox.com/s/8unx3knosmefedk/download%20%281%29.svg?raw=1';
      song.pause();
      video.pause();

    }
  }
};
app();
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  outline: none;
}

.app {
  height: 100vh;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}

.time-select,
.sound-picker,
.player-container {
  height: 80%;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
}

.player-container {
  position: relative;
}

.player-container svg {
  position: absolute;
  height: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.time-display {
  position: absolute;
  bottom: 10%;
  color: white;
  font-size: 50px;
}

video {
  position: fixed;
  top: 0%;
  left: 0%;
  width: 100%;
  z-index: -10;
}

.time-select button,
.sound-picker button {
  color: white;
  width: 30%;
  height: 10%;
  background: none;
  border: 2px solid white;
  cursor: pointer;
  border-radius: 5px;
  font-size: 20px;
  transition: all 0.5s ease;
}

.time-select button:hover {
  color: black;
  background: white;
}

.sound-picker button {
  border: none;
  height: 120px;
  width: 120px;
  border-radius: 50%;
}

.sound-picker button:nth-child(1) {
  background: #4972a1;
}

.sound-picker button:nth-child(2) {
  background: #a14f49;
}

.sound-picker:focus {
  outline: none;
}
<img src="https://www.dropbox.com/s/3zvnjkebwt1sjgq/download%20%283%29.svg?raw=1" style="display:none;">
<img src="https://www.dropbox.com/s/8unx3knosmefedk/download%20%281%29.svg?raw=1" style="display:none;">
<div class="app">
  <div class="vid-container">
    <video Loop>
                <source src="https://www.dropbox.com/s/wkdu9elom9o4r5g/rain%20%281%29.mp4?raw=1"/>
            </video>
  </div>
  <div class="time-select">
    <button data-time="120">2 Minutes</button>
    <button data-time="300">5 Minutes</button>
    <button data-time="600">10 Minutes</button>
  </div>
  <div class="player-container">
    <audio class="song">
                <source src="https://www.dropbox.com/s/jawlfpyyz83w2td/rain.mp3?raw=1"></source>
            </audio>
    <img src="https://www.dropbox.com/s/8unx3knosmefedk/download%20%281%29.svg?raw=1" class="play-container" alt="">
    <svg class="track-outline" width="453" height="453" viewBox="0 0 453 453" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="226.5" cy="226.5" r="216.5" stroke="white" stroke-width="20"/>
    </svg>
    <svg class="moving-outline" width="453" height="453" viewBox="0 0 453 453" fill="none" xmlns="http://www.w3.org/2000/svg">
    <circle cx="226.5" cy="226.5" r="216.5" stroke="#018EBA" stroke-width="20"/>
</svg>
    <h3 class="time-display">0:00</h3>
  </div>
  <div class="sound-picker">
    <button data-sound="https://www.dropbox.com/s/jawlfpyyz83w2td/rain.mp3?raw=1" data-video="https://www.dropbox.com/s/wkdu9elom9o4r5g/rain%20%281%29.mp4?raw=1"><img src="https://i.ibb.co/8BspYTV/rain-1.png"></button>
    <button data-sound="https://www.dropbox.com/s/6k9nauf2ffyvfuu/beach.mp3?raw=1" data-video="https://www.dropbox.com/s/tsdd86bxmax32jp/beach.mp4?raw=1"><img src="https://i.ibb.co/T0xw4k7/sun-umbrella.png"></button>


  </div>