Comments (12)
Hi there,
There is bound to be some code in here to help you:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Video_and_audio_APIs
Basically, you need a video element:
<video src="myVideo.mp4">
And a button
<button>Play</button>
And some code to wire up the button to the video
let videoElem = document.querySelector('video');
let playBtn = document.querySelector('button');
playBtn.addEventListener('click', function() {
videoElem.play();
})
If you want to make the button look nice, for example overlaying on top of the video, you'll need to use some CSS to style it, perhaps some absolute positioning to position it on the top?
Hope this helps.
from learning-area.
This worked fine for me:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>VR Salud</title>
<style>
html, body { margin: 0; padding:0; overflow: hidden; }
h1 { color:white; position:absolute; top:95%; z-index:100; width:100%; text-align: center;; transform: translate(0,-100%); font-family: 'Raleway', sans-serif; font-weight: 100; letter-spacing: 1px; text-transform: lowercase; font-size: 30px; }
</style>
</head>
<body>
<video width="640" height="320" autoplay loop>
<source src="rabbit320.mp4" type="video/mp4">
<p>Your browser doesn't support HTML5 video.</p>
</video>
<button>Play</button>
<script> let videoElem = document.querySelector('video'); let playBtn = document.querySelector('button'); playBtn.addEventListener('click', function() { videoElem.play(); }); </script>
</body>
</html>
It was probably the video format β I don't think we support WebM any more, and you can play mp4 anywhere.
from learning-area.
I think this is just because your video is too big and so is pushing the button offscreen?
You could get the button onscreen easily by just putting it before the video in the source order.
Or you could use absolute positioning to position it over the video, for example
button {
position: absolute;
top: 0;
left: 0;
}
by default, absolute position positions elements relative to the <body>
element. If you want to have them positioned relative to a different element, you'll need to nest them inside that element, and make sure that element has positioning set on it (like relative
), so that the positioning context becomes that element, and not the body element.
So for example:
<div class="container">
<video width="640" height="320" autoplay loop>
<source src="rabbit320.mp4" type="video/mp4">
<p>Your browser doesn't support HTML5 video.</p>
</video>
<button>Play</button>
</div>
And some CSS:
.container {
position: relative;
}
button {
position: absolute;
top: 0;
left: 0;
}
The video would then site inside the container, and the button would be positioned relative to the container's edges, regardless of there the container ends up on the page.
from learning-area.
The correct properties to use are top, bottom, left, right.
The following tutorial should help:
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Positioning
from learning-area.
Hi chrisdavidmills
I will try it !!!
Thank you for your help !!!
Alejandro
from learning-area.
Hi chrisdavidmills,
Sorry again for my ignorance but I added what you told me and it is not working for me. I cannot the Play button in my background. Surely, I am making a mistake...Could you help me to fix it? Thanks for your time
My code is the following
`
<title>VR Salud</title>
<style>
html, body { margin: 0; padding:0; overflow: hidden; }
h1 { color:white; position:absolute; top:95%; z-index:100; width:100%; text-align: center;; transform: translate(0,-100%); font-family: 'Raleway', sans-serif; font-weight: 100; letter-spacing: 1px; text-transform: lowercase; font-size: 30px; }
#videoBacker { background-size: cover; object-fit: cover; z-index: 9; opacity:.3; position: absolute; top:0px; left:0px; width:100vw; height: 100vh; transition: 1s opacity ease-in-out;
}
</style> <video width="1920" height="1080" controls autoplay loop>
<source src="Hexxcaudio.webm" type="video/webm">
<p>Your browser doesn't support HTML5 video. Here is a <a href="Hexxcaudio.webm">link to the video</a> instead.</p>
</video>
<button>Play</button>
from learning-area.
Hi again
Great !!!
It is working well if I add the 640 x 320 px ( I can see the Play button and see the video and audio)
I do not want to abuse of your generosity but since that my video is 1920 x 1080, px ( I cannot the Play button). for it, Do you know how I could add the Play button above the video and click the button to see it? Would it be possible? One more time, thanks for your help. Alejandro
from learning-area.
Hi
It is working !!
I did what you told me and also linked the CSS file in my index.htmI file with the following code
from learning-area.
Hello chrisdavidmills
I apologize for bothering you again but I have a problem with the Video in my website because it is not working like Responsive Design, for it, Could you help me with it? Thanks for your time
from learning-area.
@castana1962 Could you please be more informative about your issue? I mean like, providing some of your code (you probably need jsfiddle.net or pastebin.com if its too long), screenshot of your site, or maybe like your console log.
from learning-area.
@castana1962 I missed your last query completely - really sorry!
To learn about responsive design, start by reading https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Responsive_Design
You can get replaced elements like <img>
and <video>
to behave responsively pretty cheaply. Usually something like this will work:
img, video, iframe {
display: block;
max-width: 100%;
}
from learning-area.
@castana1962 / @chrisdavidmills it seems like this can be closed, if so kindly close
from learning-area.
Related Issues (20)
- Form example looks ugly. HOT 7
- oojs.html example shows HTML0204 lint warning HOT 5
- Learning area
- Left sidebar doesn't show scrollbar at specific font sizes. HOT 1
- html/forms/html-form-structure/payment-form.html STYLING ISSUE HOT 3
- Guides > Tools and testing > Cross browser testing > Handling common HTML and CSS problems (Issue)
- [revise] fix payment form code snippet HOT 1
- [type="number"]
- Test your skills: Advanced HTML text β Task 2, paragraph 2: Copy discrepancy HOT 19
- Error Message is Cut Off at the Top when the Example is at the Bottom of the Browser Window HOT 10
- javascript/introduction-to-js-1/first-splash/number-guessing-game.html line 64 is different than any material covered by the tutorial HOT 3
- Standardize lang attribute usage in HTML tutorial on MDN to 'en-US' or 'en-us' HOT 3
- Auto-generated method stub
- Auto-generated method stub
- Width capped at 90% and height capped at 160px in javascript/building-blocks/tasks/styles.css HOT 3
- Label is covered by Text Area in learning-area/html/forms/your-first-HTML-form/first-form-styled.html
- My first issue HOT 1
- new day
- Site
- Where are my divs? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learning-area.