问题
I have a sidebar which displays the list of movies from a JSON file, here si visual how it looks.
Now I want when a user clicks edit button one of the movie in a list it should open a popup modal with a movie block, something like this.
Here si live demo jsfiddle: live demo
Here is what I have tried so far
HTML
<ul class="sidebar">
</ul>
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
</div>
</div>
Here is js
$(function() {
var movies = [{
"title": "travel",
"left": 201,
"top": 209,
"movieid": "10",
"movie_url": "http://techslides.com/demos/sample-videos/small.mp4",
"buttons": [{
"left": 81,
"top": 51,
"start_time": 1,
"end_time": 2,
"buttonid": "10_1",
"btn_url": "http://techslides.com/demos/sample-videos/small.mp4"
}]
},
{
"title": "ecommerce",
"movieid": "20",
"movie_url": "http://techslides.com/demos/sample-videos/small.mp4",
"buttons": [{
"left": 0,
"top": 0,
"start_time": 1,
"end_time": 2,
"width": '200',
"height": '60',
"buttonid": "20_1",
}]
}
];
function formatTitle(t) {
var nt = t[0].toUpperCase();
nt += t.slice(1);
return nt;
}
function makeListItem(v, p) {
var li = $("<div id='" + v.movieid + "' class='sidebar_movie-block'>");
var title = $("<h1>", {
class: "title",
for: "video_" + v.movieid
}).html(formatTitle(v.title)).appendTo(li);
var edit = $("<span>", {
class: "block-edit fa fa-edit",
for: "video_" + v.movieid,
}).appendTo(li);
var vObj = $("<video>", {
id: "video_" + v.movieid,
src: v.movie_url
}).appendTo(li);
li.appendTo(p);
}
function getVideoList() {
$.each(movies, function(index, dataValue) {
makeListItem(dataValue, $(".sidebar"));
});
}
getVideoList();
var modal = $("#myModal");
// When the user clicks the button, open the modal
$(".block-edit").on("click", function() {
$("#myModal").css("display", "flex");
})
// When the user clicks on <span> (x), close the modal
$(".close").on("click", function() {
$("#myModal").css("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";
}
}
});
I am stuck to display the movie inside a pop modal after I click edit, I tried different ways but unfortunately am out of ideas.
What do I need to change to get what I want? any suggestion or help will be apreciated
回答1:
Sorry for the layout, but all I did was add a brother element of the modal close button called "modalVideo" and another called "modalTitle" for title:
<ul class="sidebar">
</ul>
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<h1 id="modalTitle"></h1>
<span class="close">×</span>
<div id="modalVideo">
</div>
</div>
</div>
I added this function, which is triggered in the click of the video and adds it in the modal.
$("video").on("click", function(cls){
$("#myModal").css("display", "flex");
//get video tag and put into modal.
$("#modalVideo")[0].innerHTML = cls.target.outerHTML
})
If you want to switch to the click of the button, just change the $("video")
to
desired element.
Here is the fiddler link: https://jsfiddle.net/z4v18y3g/
[EDIT]
New fiddler with title: https://jsfiddle.net/z4v18y3g/2/
来源:https://stackoverflow.com/questions/56675821/how-to-open-a-clicked-video-block-to-a-pop-modal