Атрибут autoplay тега <video> — автозапуск відео
Атрибут autoplay - логічний атрибут.
Якщо він присутній, відео автоматично почне відтворюватися, як тільки це буде можливо зробити.
автор: с3с
Загальний опис
автор: NagarD
- Нотатка
-
З використання атрибуту
автор: с3сautoplayбез використанняcontrolsвідео неможливо зупинити(можна використати як рекламу).
Синтаксис
<video autoplay>
Переглядачі
| Переглядач | ||||||
|---|---|---|---|---|---|---|
| 3 | 3.5 | 3.1 | 10.5 | 12 | 9 |
| Переглядач | ||||
|---|---|---|---|---|
| 4.4 | 18 | 4 | 10 |
Приклади
+ запропонувати свій приклад у пісочниці
HTML
<div class="block1" >
<p> З використанням атрибутів <code> autoplay</code> та <code>controls</code> </p>
<video width="240" height="135" controls autoplay="autoplay" poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='135' viewBox='0 0 64 36' preserveAspectRatio='none'%3E%3Crect width='64' height='36' fill='%23264653'/%3E%3Ccircle cx='32' cy='18' r='10' fill='%23e9c46a'/%3E%3Cpath d='M29 12 38 18 29 24Z' fill='%23264653'/%3E%3C/svg%3E">
Ваш браузер не відтворює відео.
</video>
</div>
<div class="block1" >
<p> З використання атрибуту <code> autoplay</code> без <code>controls</code> </p>
<video width="240" height="135" autoplay poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='135' viewBox='0 0 64 36' preserveAspectRatio='none'%3E%3Crect width='64' height='36' fill='%23264653'/%3E%3Ccircle cx='32' cy='18' r='10' fill='%23e9c46a'/%3E%3Cpath d='M29 12 38 18 29 24Z' fill='%23264653'/%3E%3C/svg%3E">
Ваш браузер не відтворює відео.
</video>
</div>
<div class="block1" >
<p> Без використання атрибутів <code> autoplay</code> та <code>controls</code> </p>
<video width="240" height="135" preload="auto" poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='135' viewBox='0 0 64 36' preserveAspectRatio='none'%3E%3Crect width='64' height='36' fill='%23264653'/%3E%3Ccircle cx='32' cy='18' r='10' fill='%23e9c46a'/%3E%3Cpath d='M29 12 38 18 29 24Z' fill='%23264653'/%3E%3C/svg%3E">
Ваш браузер не відтворює відео.
</video>
</div>
<div class="block1" >
<p> Без використання атрибуту <code> autoplay</code> з <code>controls</code> </p>
<video width="240" height="135" preload="auto" controls poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='135' viewBox='0 0 64 36' preserveAspectRatio='none'%3E%3Crect width='64' height='36' fill='%23264653'/%3E%3Ccircle cx='32' cy='18' r='10' fill='%23e9c46a'/%3E%3Cpath d='M29 12 38 18 29 24Z' fill='%23264653'/%3E%3C/svg%3E">
Ваш браузер не відтворює відео.
</video>
</div>
CSS
.block1{
float:left;
padding:10px;
border:2px solid black;
}
Чотири однакові плеєри, різні набори атрибутів. Підпис під кожним зібрано селекторами video[autoplay] і video[autoplay]:not([muted]) — видно, що autoplay="false" браузер читає як УВІМКНЕНО, а без muted автостарт буде заблоковано. Замість відеофайлу — постер із inline-SVG.
HTML
<div class="row">
<figure>
<video muted loop playsinline poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 180'%3E%3Crect width='320' height='180' fill='%23202b38'/%3E%3Cpolygon points='140,62 140,118 188,90' fill='%23ffffff'/%3E%3C/svg%3E"></video>
<figcaption><video muted loop playsinline></figcaption>
</figure>
<figure>
<video autoplay="false" muted playsinline poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 180'%3E%3Crect width='320' height='180' fill='%23202b38'/%3E%3Cpolygon points='140,62 140,118 188,90' fill='%23ffffff'/%3E%3C/svg%3E"></video>
<figcaption><video autoplay="false" muted></figcaption>
</figure>
<figure>
<video autoplay playsinline poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 180'%3E%3Crect width='320' height='180' fill='%23202b38'/%3E%3Cpolygon points='140,62 140,118 188,90' fill='%23ffffff'/%3E%3C/svg%3E"></video>
<figcaption><video autoplay> без muted</figcaption>
</figure>
<figure>
<video autoplay muted loop playsinline poster="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 180'%3E%3Crect width='320' height='180' fill='%23202b38'/%3E%3Cpolygon points='140,62 140,118 188,90' fill='%23ffffff'/%3E%3C/svg%3E"></video>
<figcaption><video autoplay muted loop playsinline></figcaption>
</figure>
</div>
CSS
body {
font-family: system-ui, sans-serif;
font-size: 15px;
}
.row {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
figure {
margin: 0;
width: 240px;
}
video {
display: block;
width: 240px;
height: 135px;
background: #000;
}
figcaption {
margin-top: 6px;
font-family: ui-monospace, monospace;
font-size: 12px;
line-height: 1.5;
color: #555;
}
figure:has(video:not([autoplay])) video {
outline: 3px solid #2e7d32;
}
figure:has(video:not([autoplay])) figcaption::after {
display: block;
content: 'автовідтворення вимкнено';
color: #2e7d32;
}
figure:has(video[autoplay]) video {
outline: 3px solid #b8860b;
}
figure:has(video[autoplay]) figcaption::after {
display: block;
content: 'браузер читає це як АВТОВІДТВОРЕННЯ УВІМКНЕНО';
color: #b8860b;
}
figure:has(video[autoplay]:not([muted])) video {
outline: 3px solid #c0392b;
}
figure:has(video[autoplay]:not([muted])) figcaption::after {
display: block;
content: 'увімкнено, але без muted — браузер заблокує';
color: #c0392b;
}
Різниця між HTML 4.01 та HTML5
Атрибут autoplay з'явився у HTML5.
Різниця між XHTML та HTML
У XHTML мінімізований запис атрибутів заборонено, тому атрибут autoplay повинен бути визначений як <video autoplay="autoplay">.
Коментарі
Коментарів ще немає — будьте першим!