【问题标题】:How to load a iframe only on button click如何仅在单击按钮时加载 iframe
【发布时间】:2017-05-30 15:25:36
【问题描述】:

仅当单击“观看视频”按钮时,我才想要一个 youtube 视频 Iframe。当前,当页面加载时,视频 iframe 在后台加载。

注意:我不是说,点击按钮播放视频。我的意思是,在单击按钮时加载 iframe

https://jsfiddle.net/kz0xxe22/

// Get the modal
var modal = document.getElementById('trailerdivbox');

// Get the button that opens the modal
var btn = document.getElementById("watchbutton");

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

var trailerbox = document.getElementById("close");

trailerbox.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
/* Watch Trailer Button CSS */

#watchbutton {
  background-color: #f2f2f2;
  color: red;
  font-weight: 600;
  border: none;
  /* This one removes the border of button */
  padding: 10px 12px;
}

#watchbutton:hover {
  background-color: #e2e2e2;
  cursor: pointer;
}

#trailerdivbox {
  display: none;
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: auto;
  /* Enable Scrolling */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}

.videoWrapper {
  position: relative;
  padding-bottom: 56.25%;
  /* 16:9 */
  padding-top: 25px;
  height: 0;
}

.videoWrapper iframe {
  position: absolute;
  max-width: 560px;
  max-height: 315px;
  width: 95%;
  height: 95%;
  left: 0;
  right: 0;
  margin: auto;
}
<button id="watchbutton">Watch Trailer &#9658</button>

<div id="close">
  <div id="trailerdivbox">
    <div class="videoWrapper">
      <iframe class="trailervideo" width="560" height="315" src="https://www.youtube.com/embed/TDwJDRbSYbw" frameborder="0" allowfullscreen></iframe>
    </div>
  </div>
</div>

【问题讨论】:

标签: javascript jquery html css iframe


【解决方案1】:

您可以将src 属性更改为data-src,然后当您单击按钮时,将src 设置为data-src

// Get the modal
var modal = document.getElementById('trailerdivbox');

// Get the button that opens the modal
var btn = document.getElementById("watchbutton");

var trailer = document.getElementById('trailervideo');

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
  trailer.setAttribute('src', trailer.getAttribute('data-src'));
}

var trailerbox = document.getElementById("close");

trailerbox.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
/* Watch Trailer Button CSS */

#watchbutton {
  background-color: #f2f2f2;
  color: red;
  font-weight: 600;
  border: none;
  /* This one removes the border of button */
  padding: 10px 12px;
}

#watchbutton:hover {
  background-color: #e2e2e2;
  cursor: pointer;
}

#trailerdivbox {
  display: none;
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: auto;
  /* Enable Scrolling */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}

.videoWrapper {
  position: relative;
  padding-bottom: 56.25%;
  /* 16:9 */
  padding-top: 25px;
  height: 0;
}

.videoWrapper iframe {
  position: absolute;
  max-width: 560px;
  max-height: 315px;
  width: 95%;
  height: 95%;
  left: 0;
  right: 0;
  margin: auto;
}
<button id="watchbutton">Watch Trailer &#9658</button>


<div id="close">
  <div id="trailerdivbox">
    <div class="videoWrapper">
      <iframe id="trailervideo" class="trailervideo" width="560" height="315" data-src="https://www.youtube.com/embed/TDwJDRbSYbw" frameborder="0" allowfullscreen></iframe>
    </div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    最简单的做法是在单击“观看预告片”按钮时添加src 属性! :)

    // Get the modal
    var modal = document.getElementById('trailerdivbox');
    
    // Get the button that opens the modal
    var btn = document.getElementById("watchbutton");
    
    
    
    // When the user clicks the button, open the modal 
    btn.onclick = function() {
        modal.style.display = "block";
    
    
    
        //Add "src" attribute by getting the video and setting it with setAttribute.
        document.getElementsByClassName('trailervideo')[0].setAttribute('src', 'https://www.youtube.com/embed/TDwJDRbSYbw');
    
    
    
    }
    
    var trailerbox = document.getElementById("close");
    
    trailerbox.onclick = function() {
        modal.style.display = "none";
    }
    
    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
    /* Watch Trailer Button CSS */
    #watchbutton {
    	background-color:#f2f2f2;
    	color:red;
    	font-weight:600;
    	border: none; /* This one removes the border of button */
    	padding: 10px 12px;
    
    }
    
    #watchbutton:hover {
    	background-color:#e2e2e2;
    	cursor:pointer;
    }
    
    #trailerdivbox {
    	display:none;
    	width: 100%;
    	height:100%;
    	position:fixed;
    	overflow: auto; /* Enable Scrolling */
    	background-color: rgb(0,0,0); /* Fallback color */
        background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    	
    	
    }
    
    .videoWrapper {
    	position: relative;
    	padding-bottom: 56.25%; /* 16:9 */
    	padding-top: 25px;
    	height: 0;
    }
    .videoWrapper iframe {
    	position: absolute;
    	max-width:560px;
    	max-height:315px;
    	width: 95%;
    	height: 95%;
    	left:0;
    	right:0;
    	margin:auto;
    }
    <button id="watchbutton">Watch Trailer &#9658</button>
    
    
    <div id="close">
    <div id="trailerdivbox" >
    <div class="videoWrapper">
    <iframe class="trailervideo" width="560" height="315" frameborder="0" allowfullscreen></iframe>
    </div>
    </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2017-10-07
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多