HTML5: How to Move/Change a Video's Position with a Button - javascript

I am fairly new to HTML5 and Javascript. I have only created simple websites so I apologize in advance for my limited knowledge. A friend needs some help and I offered to take a crack at it.
The concept is simple enough:
-Have a button that will adjust the position of a video up or down.
-The video should be contained inside a window. The window will hold a smaller resolution compared to the video, in order to hide the rest of the video and only reveal the portion within the window.
Reference images to help illustrate the idea are below (I can only post 2):
Ref-Button1
Ref-Button2
Is this possible?
Through researching, I am under the impression that a moveable div or possibly a canvas might be the right approach.
So far, all of my attempts are failing. I don't think it will help much, but an example of my script is also provided if needed.
Any insight is greatly appreciated.
<style>
body {
background-color: Grey
color: #CCCCCC;
}
#videoWindow {
width: 720px;
height: 350px;
margin-left: 400px;
margin-right: 400px;
border: 7px solid #0F0F0F;
border-radius: 5px;
}
#videoControls {
margin-left: 400px;
margin-right: 400px;
position: fixed;
background-color: #FFFFFF;
width: 400px;
height: 250px;
}
#videoManip {
width: 720px;
height: 350px;
}
</style>
<body>
<div>
<!--Video Window (Static)-->
<div id="videoWindow" class="videoWindow">
<!--Video Re-Positioner-->
<div id="videoManip" class="videoManip">
<!--Video-->
<video id="video" width="720" height="1050">
<source src="Videos/VideoPlayerTest.mp4" type="video/mp4">
</div>
<!--VideoPositionControls-->
<div class=""><!--SetClass-->
<!--Button1-->
<input type="button" id="button1" value="Button1">
<!--Button2-->
<input type="button" id="button2" value="Button2">
<!--Button3-->
<input type="button" id="button3" value="Button3">
</div>
</div>
</body>
<script>
//Video
var video = document.getElementById("video");
//VideoPositionControls
var Btn1 = document.getElementById("button1");
var Btn2 = document.getElementById("button2");
var Btn3 = document.getElementById("button3");
//Event Listener: Video Position Controls
//Update Video Position for 'Cam1'
Btn1.addEventListener("click", function() {
video.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(0,0)
//Update Video Position for 'Cam2'
Btn2.addEventListener("click", function() {
video.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(0,350)
//Update Video Position for 'Cam3'
Btn3.addEventListener("click", function() {
video.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(0,-350)
</script>

The most easy way to do that is to just move video vertically inside its parent.
Addition to CSS:
#videoManip {
overflow: hidden;
}
#video {
position: relative;
}
Addition to javascript:
Btn1.addEventListener("click", function() {
video.style.top = 0;
});
Btn2.addEventListener("click", function() {
video.style.top = '-350px';
});
Btn3.addEventListener("click", function() {
video.style.top = '-700px';
});
Here is tiny jsfiddle illustrating that: https://jsfiddle.net/iStyx/ocgx6pdL/

You could use JQuery to achieve that.
First define two buttons with an onclick event.
<div id="videoControls">
<button id="move-up" onclick="move(0,1)">Up</button>
<button id="move-down" onclick="move(0,-1)">Down</button>
</div>
The following code handles the actual positioning with jquery. The function move takes 2 parameters x and y for the number of pixels the video should move along the axis. In this example it uses margin for positioning. You should check if it's good with your use-case.
const STEP = 10;
const VIDEO_ID="video";
function move(x,y){
var valx = "+="+(STEP*x)+"px";
var valy = "+="+(STEP*y)+"px";
$(VIDEO_ID).css( 'margin-top', valy );
$(VIDEO_ID).css( 'margin-left', valx );
}

Related

Hide Nav When Video is playing with js

I have a client who built their site with wpbakery and on their posts are using video player. They want to have it so when the video plays the nav bar disappears. In the inspector I can see that the player has a class of 'video-container' and when the video is playing there is an addition class of 'playing'. I am hoping to check for if video-container has playing then add a class to my nav.
var hideNav = document.getElementsByClassName('video-container playing');
if (hideNav) {
document.getElementByClassName("nav").className = "hidden";
}
I am guessing I need to add an event listener and I am not sure if my hideNav variable is working how I want it to.
Here's an answer that doesn't use jQuery:
const videoEl = document.getElementById('video')
const navbarEl = document.getElementById('navbar')
videoEl.addEventListener('play', () => {
navbarEl.classList.add('hidden')
})
videoEl.addEventListener('pause', () => {
navbarEl.classList.remove('hidden')
})
.hidden {
display: none;
}
<div style="padding: 30px 10px; width: 100%; background: gray; margin-bottom: 20px;" id="navbar">
Navbar
</div>
<video src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" id="video" controls width="400" />

Why getting a preload box for a img tag?

I am getting a preload box for a img tag from the code below.
<script>
document.addEventListener('DOMContentLoaded', function(){
load()
}, false);
function grab(){
var urlsource = document.getElementById("image").value="";
return urlsource.length > 0;
}
function load(){
var imgurl = document.getElementById("image").value;
document.getElementById("replace").src=imgurl;
return imgurl.length > 0;
}
</script>
<style>
div.output {
height: 200px;
width: 200px; }
img#replace {
display: none;
}
img#replace[src]{
height: 150px;
width: 150px;
position: absolute;
display: inline-block;
}
</style>
</head>
<body>
<textarea id="image" onload="grab();" onkeyUp="load();" onkeyPress="load();"></textarea><br>
<a id="demo1" href="https://www.google.co.nz/">
<div class="output">
<img id="replace"/>
</div>
</a>
</body>
How the preload image can be removed while keeping the DOMContentloaded event? Thanks very much!
Every <img/> must have a src. If it does not, it's treated as broken.
Give it a src, even if that means creating a 1x1 transparent GIF pixel.
<img id="replace" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />
EDIT: On closer inspection, it seems that your code is immediately running when the document has loaded, with the code effectively reading:
image.src = ""; // because the textarea is empty
Of course, this is not a valid image, but it is still an image, so it appears as a broken one. You may want to add an onerror event to the image to hide it if it fails to load.
<img id="replace" onerror="this.removeAttribute('src');" />
(This should work because of your CSS hiding source-less images)

Can each new click produce a new image?

I know how to make an image appear using onclick or onMouseOver but how can I make each click produce the appropriate image not just in the same place but, for example, in a row, next to it's previous apperance? My idea is this: I click on reference1 and a picture1 shows up. Then I click on reference2 and a picture2 shows up next to the picture1 already displayed. Now I click on reference1 again, and I want to see pictures 1,2,1 in a row. How can I do that? My ideal would be to see the row rolling when filled, and a delete button deleting the last image in that row, even making the pictures jump out being called from the text field, but I can search for these myself. My greatest concern for now is new click=new image. Thank you.
Assuming this is relatively simplistic- you could keep track of the current position in a list of images, afterwards create a function that deals with the current image then increments this position. Have the onClick event call this function, and there you are.
An example of this in action, using JQuery, can be viewed here:
http://jsfiddle.net/8Q4LQ/
Here's an example.
<html>
<head>
<style>
.refimg { width: 100px; height: 100px; }
.choices a { margin-right: 2ex; }
.choices img { display: none; }
#target { display:block; width: 500px; overflow-x: scroll; border: 1px solid black; }
</style>
</head>
<body>
Choices:
<div class="choices">
ref1
ref2
<image id="image1" src="image1.gif" class="refimg" />
<image id="image2" src="image2.gif" class="refimg" />
</div>
<br />
Selections: <input type="button" value="Delete" onclick="delImage()" />
<nobr id="target">
</nobr>
<script>
function putImage(src) {
var a = src.cloneNode(true);
a.id = ''; //clear id to prevent duplicate id
target.appendChild(a);
a.scrollIntoView(true);
return false;
}
function delImage() {
var a=target.children;
if (a.length>0) target.removeChild(a[a.length-1]);
}
target.style.height=((target.offsetHeight-target.clientHeight)+100)+'px'; //extend height for scroll bar
</script>
</body>
</html>

Is redrawing the contents of a DIV by means of a function call possible?

Just an amateur/hobbyist here - what this is supposed to do is be a tool for a board game I play with friends. The plastic sliders the game uses are too loose to be reliable so I wanted to reproduce that functionality as a webpage to use on a smartphone while playing.
It gets a character's name from a form (on another page) and supplies it to
the one below. Based on the name, it chooses the right set of attributes from the switch statement (I removed all but two cases for the sake of simplicity), runs through a for loop to display the attributes in a list and highlight the "current" value as green. Two buttons are supposed to increase or decrease the array counter ("speed"), and rerun the function that draws the array with the new highlighted value. innerHTML is meant to redraw the div ("speeddiv") with the new results.
Now the javascript console in chrome is telling me that speedcounter() and character are undefined. I suspect this has something to do with the scope of the function and variables I'm using being lost through innerhtml. All I want to do is find a way to easily redraw/replace the stat counter so it appears that the highlighted number is moving up and down as you press the + or - buttons, within the div.
I'm only working on the "speed" attribute below, so I can get that working first.
<!DOCTYPE html>
<html>
<body>
<style type="text/css">
html, body { height: 100%; width: 100%; margin: 0; }
#dossier {height: 10%; text-align: center; background: #808080}
#container {height: 90%; width: 100%; background: #000000; overflow: hidden; float: left}
#stats {height: 100%; width: 100%; float: left; position: relative}
#speeddiv, #mightdiv, #sanitydiv, #knowledgediv {width: 25%; height: 100%; text-align: center; float: left; position: relative; overflow: hidden}
#speeddiv {background: #0000FF}
#mightdiv {background: #FF0000}
#sanitydiv {background: #FFFF00}
#knowledgediv {background: #00FF00}
</style>
<?php $character = $_GET["character"]; ?>
<script type="text/javascript">
var character = "<?php echo $character ?>";
var sp;
var speed;
function speedcounter() {
var spx;
document.write(' <h2>Speed</h2></br>');
document.write('<input type="button" onclick="addspeed();" value="+"><br />');
for (spx=8; spx>=0; spx--) {
if (spx == speed) {
document.write('<font color=#00FF00>');
}
document.write(sp[spx]);
document.write('<font color=#000000><br />');
}
document.write ('<input type="button" onclick="remspeed();" value="-">');
}
function addspeed() {
if (speed < 8) {
speed++;
document.getElementById("speeddiv").innerHTML = "<script type="text/javascript">speedcounter();<\/script>";
}
}
function remspeed() {
if (speed > 0) {
speed--;
document.getElementById("speeddiv").innerHTML = "<script type="text/javascript">speedcounter();<\/script>";
}
}
switch (character) {
case "brandon":
sp=["0","3","4","4","4","5","6","7","8"];
mt=["0","2","3","3","4","5","6","6","7"];
sn=["0","3","3","3","4","5","6","7","8"];
kn=["0","1","3","3","5","5","6","6","7"];
speed=3;
might=4;
sanity=4;
knowledge=3;
break;
case "flash":
sp=["0","4","4","4","5","6","7","7","8"];
mt=["0","2","3","3","4","5","6","6","7"];
sn=["0","1","2","3","4","5","5","5","7"];
kn=["0","2","3","3","4","5","5","5","7"];
speed=5;
might=3;
sanity=3;
knowledge=3;
break;
}
</script>
<div id="dossier">
<script type="text/javascript">
document.write(character);
</script>
</div>
<div id="container">
<div id="stats">
<div id="speeddiv">
<script type="text/javascript">
speedcounter();
</script>
</div>
<div id="mightdiv">
<h2>Might</h2></br></br>
</div>
<div id="sanitydiv">
<h2>Sanity</h2></br></br>
</div>
<div id="knowledgediv">
<h2>Knowledge</h2></br></br>
</div>
</div>
</div>
</script>
</body>
</html>
I've created a jsfiddle from the code you've posted http://jsfiddle.net/amelvin/bwwce/ - working on it interactively in there may help.
I think your problem is what is happening with the document.write; the section on document.write explains that what document.write does is not very predictable.
Use a javascript library like jquery to insert elements into the webpage rather than document.write - the html() method in jquery (amongst others) allows you dynamically and predictably manipulate any aspect of the page based on events like button pushes, adding or removing buttons or divs.

object positioning with style.position

I want to be able to move an object from one position to another using buttons for example 3 buttons left right and center that always put the object at the exact same positions everytime.
I have tried using style.position="absolute" but it moves left or right or whatever position depending on it last position never the same three positions.
Which one of static, absolute, fixed, relative, inherit would be best to use in this case? and is it possible to get an example of how a particular object would be set to a particular position thanks in advance
position: absolute tells the browser HOW to position your element, but not WHERE to position your element. You still need to position your element by setting left or right and top or bottom values in your css.
Given some markup:
<button id="left">LEFT</button>
<button id="right">RIGHT</button>
<button id="center">CENTER</button>
<div id="wrapper">
<div id="thingy"/>
</div>​
and some styles:
#wrapper {
position: relative;
margin-top: 10px;
}
#thingy {
margin: 0 auto;
width: 25px;
height: 25px;
background-color: #f69;
}​
You can move the thingy this way:
var thingy = document.getElementById('thingy');
document.getElementById('left').onclick = function() {
thingy.style.position = 'absolute';
thingy.style.right = null;
thingy.style.left = 0;
};
document.getElementById('right').onclick = function() {
thingy.style.position = 'absolute';
thingy.style.left = null;
thingy.style.right = 0;
};
document.getElementById('center').onclick = function() {
thingy.style.position = 'inherit';
thingy.style.left = null;
thingy.style.right = null;
};
​
Code is posted at: http://jsfiddle.net/TXWfh/1/
You could make it work with absolute, but I think relative might work best in your case.
HTML
<div id="test"></div>
<input type="button" id="left" value="Left">
<input type="button" id="middle" value="Middle">
<input type="button" id="right" value="Right">
CSS
#test {
position: relative;
width: 100px;
height: 100px;
background-color: red;
}
input {
padding: 4px;
}
JavaScript
var test = document.getElementById("test");
document.getElementById("left").onclick = function() {
test.style.left = "0px";
};
document.getElementById("middle").onclick = function() {
test.style.left = "250px";
};
document.getElementById("right").onclick = function() {
test.style.left = "500px";
};
Live example
Don't know what you are trying to do, but it really doesn't matter if you don't have a lot of surrounding html code that can get affected by your choice of position value.
following 3 fiddles do same things but with subtle difference that they have different value for position attribute.
http://jsfiddle.net/9uQT8/3/
http://jsfiddle.net/9uQT8/4/
http://jsfiddle.net/9uQT8/5/
try clicking left center right. and see. I used jQuery though, super cool JS framework.

Categories

Resources