admin管理员组文章数量:1287791
I am using videojs to build a website to play videos and insert some images to the page at certain time point of the video being played. I used the code below to get the current time and insert an image in "imageContext" div.
<script type="text/javascript">
var myPlayer = document.getElementById("example_video_1");
var added = false;
var ShowAds=function(){
var time = myPlayer.currentTime;
var img = document.createElement('IMG');
div = document.getElementById("imageContext");
div.style.width = '100px';
div.style.height = '100px';
div.style.display = 'inline-block';
if(time > 30 && time <= 40 && !added){
//img.onload = function(){
div.appendChild(img);
added = true;
img.setAttribute("src",'/Applications/MAMP/htdocs/shqc.jpg');
img.style.width = '100%';
img.style.height = 'auto';
}else if(time > 70){
//change to another image in the same position
}
}
myPlayer.addEventListener('timeupdate',ShowAds,false);
</script>
What if I want to show another image on the page when the time changes to, say, the 90th second? Or is there any way to delete the image on the page using javascript? Thanks!
To be clear, I have tried to put
img.setAttribute("src",'/Applications/MAMP/htdocs/yy.jpeg');
under else, it doesn't work
I am using videojs to build a website to play videos and insert some images to the page at certain time point of the video being played. I used the code below to get the current time and insert an image in "imageContext" div.
<script type="text/javascript">
var myPlayer = document.getElementById("example_video_1");
var added = false;
var ShowAds=function(){
var time = myPlayer.currentTime;
var img = document.createElement('IMG');
div = document.getElementById("imageContext");
div.style.width = '100px';
div.style.height = '100px';
div.style.display = 'inline-block';
if(time > 30 && time <= 40 && !added){
//img.onload = function(){
div.appendChild(img);
added = true;
img.setAttribute("src",'/Applications/MAMP/htdocs/shqc.jpg');
img.style.width = '100%';
img.style.height = 'auto';
}else if(time > 70){
//change to another image in the same position
}
}
myPlayer.addEventListener('timeupdate',ShowAds,false);
</script>
What if I want to show another image on the page when the time changes to, say, the 90th second? Or is there any way to delete the image on the page using javascript? Thanks!
To be clear, I have tried to put
img.setAttribute("src",'/Applications/MAMP/htdocs/yy.jpeg');
under else, it doesn't work
Share Improve this question edited Aug 21, 2015 at 15:08 Demonedge asked Aug 21, 2015 at 14:01 DemonedgeDemonedge 1,4234 gold badges21 silver badges35 bronze badges 7-
4
Then just do another
img.src = 'url'
when time is 90 – Patrick Evans Commented Aug 21, 2015 at 14:04 - possible duplicate of Programmatically change the src of an img tag – Maytham Fahmi Commented Aug 21, 2015 at 14:09
- Just a style note: It is generally remended that only constructor functions be named starting with a capital letter. – BCDeWitt Commented Aug 21, 2015 at 14:10
- What's up with using a path on your filesystem ("/Applications/MAMP/htdocs/shqc.jpg") as your image src? Are you not serving this off of a local server? – Josiah Keller Commented Aug 21, 2015 at 14:16
- no, another img.src = 'url' doesn't work – Demonedge Commented Aug 21, 2015 at 15:09
4 Answers
Reset to default 5You can set an id
to the img
element when you create it, (In case if you have multiple img
tags inside the document)
img.setAttribute('id', 'myImg');
Then, check whether time equals 90
the same way you check whether it is between 30
and 40
and change the src
of it like below.
if(time == 90 ){
document.getElementById("myImg").src="/Applications/MAMP/htdocs/your_image.jpg";
}
In case if you want to delete the
img
element, since it has a parent (imageContext
), you can do this,var el = document.getElementById('myImg'); el.parentNode.removeChild(el);
You can use this code:
document.getElementById("image").src="/PATH/To/Image/file.jpg";
I think all you need is this:
document.getElementById("myImg").src = //whatever
You could try creating and array of images locations.
var imageArray = new Array();
imageArray[0]= {
image01: new Image(),
image01.src: "my-image-01.png",
imageCaption: "An image caption"
};
Then at the next interval just set the image attribute to the src of a picture in the array or just append the whole image.
if(time > 90 && time <= 100 && !added){
div.appendChild(img);
added = true;
img.setAttribute("src",imageArray[0].src);
img.style.width = '100%';
img.style.height = 'auto';
Hope that helps. I used a little help from this other stack overflow question and answer: Creating an array of image objects
本文标签: htmlHow to change image src in a div with javascriptStack Overflow
版权声明:本文标题:html - How to change image src in a div with javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741321423a2372226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论