How to implement mouseEnter and mouseout play effect video in react? - reactjs

I am hoping someone can help me. I am trying to apply the mouseenter and mouseout effect to these videos. I am having difficulty accomplishing that, I have tried using javascript in the HTML section of my react code by calling document.querySelectorAll('.clip'), but nothing works. I am truly lost and I thank you in advance for your time.
import React from 'react';
import './Project.css'
import video from '../videos/Screen Recording 2022-04-01 at 8.22.57 PM.mov';
import video1 from '../videos/Screen Recording 2022-04-01 at 8.23.25 PM.mov';
import video2 from '../videos/Screen Recording 2022-04-01 at 8.25.20 PM.mov';
import video3 from '../videos/Screen Recording 2022-04-01 at 8.27.28 PM.mov';
import video4 from '../videos/Screen Recording 2022-04-01 at 8.28.35 PM.mov';
import video5 from '../videos/Screen Recording 2022-04-01 at 8.30.37 PM.mov';
import video6 from '../videos/Screen Recording 2022-04-01 at 8.34.30 PM.mov';
import video7 from '../videos/Screen Recording 2022-04-03 at 3.51.52 AM.mov';
import video8 from '../videos/Screen Recording 2022-04-03 at 3.48.52 AM.mov';
const Project = ()=> {
return (
<div className='project'>
<div className='container'>
<div className='project-wrapper'>
<div className='project-content'>
<h1><span className='span'>3.</span> Past Projects</h1>
<div className='project-content'>
<div className='box'>
<video
className='clip' src={video2} muted type='video/mp4' loop>
</video>
</div>
<div className='box'>
<video className='clip' src={video8} muted type='video/mp4' loop>
</video>
</div>
<div className='box'>
<video className='clip' src={video6} muted type='video/mp4' loop >
</video>
</div>
<div className='box' >
<video className='clip' src={video} muted type='video/mp4' loop >
</video>
</div>
<div className='box'>
<video className='clip' src={video4} muted type='video/mp4' loop >
</video>
</div>
<div className='box'>
<video className='clip' src={video5} muted type='video/mp4' loop >
</video>
</div>
<div className='box'>
<video className='clip' src={video3} type='video/mp4' loop >
</video>
</div>
<div className='box'>
<video className='clip' src={video7} muted type='video/mp4' loop >
</video>
</div>
<div className='box'>
<video className='clip' src={video1} muted type='video/mp4' loop >
</video>
</div>
</div>
</div>
</div>
</div>
</div>
)
}
export default Project

All you have to do is create a ref and assign the ref to a video element and onMouseEnter event play video and onMouseLeave pause video make this a component and sent video src as a prop which returns that, hope you understand what I mean.
const ref = useRef(null);
const handlePlayVideo = () => {
ref.current.play();
}
const handlePauseVideo = () => {
ref.current.pause();
}
return (<video ref={ref}>
<source src={video} type='video/mp4' onMouseEnter={handlePlayVideo} onMouseLeave={handlePauseVideo}/>)
</video>

Related

Video isn't playing in my React app when trying to import a video component and html video tag

I'm trying to import a video to my JSX but the video doesn't seem to play, it acts like an image.
Here's what I've tried:
import React from "react";
import MyVideoComponent from "./MyVideoComponent";
const Video = () => {
return (
<div className="w-full max-w-[800px] sm:h-[704px] h-[680px] bg-white flex flex-col sm:justify-between justify-center items-center mx-auto p-4">
<div>
<div className="h-[10%] flex items-center relative">
<div className="h-[100%] w-[8%] bg-gray-200"></div>
<div className="flex items-center">
<h5 className="text-sm py-2 text-[#575e72] font-mono left-9 absolute">
VIDEO TITLE
</h5>
</div>
</div>
<div className="my-5 py-4 px-1 w-[80%]">
<h1 className="text-4xl font-bold text-[#061237]">
Inform users with video sections
</h1>
</div>
<div>
<MyVideoComponent />
</div>
</div>
</div>
);
};
export default Video;
MyVideoComponent:
import React from "react";
import MyVideo from "../assets/video.mp4";
class MyVideoComponent extends React.Component {
render() {
return (
<video width="100%" height="100%" preload="auto">
<source src={MyVideo} type="video/mp4" />
Your browser does not support HTML5 video.
</video>
);
}
}
export default MyVideoComponent;
I've also tried using the code below instead of importing MyVideoComponent component
<video width="auto" height="auto" autoplay>
<source src={require('../assets/video.mp4')} />
</video>
This is common problem when you deal with html5 tag in react. You need toJust set a name for the autoplay attribute or use autoPlay. In addition, set the muted tag for the video. Chrome for instance block autoplay videos that have sound.
The code structure should look like this

Trying to render a full page video background in Next.js but getting blank page

I'm trying to have a full page video background in the homepage of my Nextjs application but am just getting blank white space where the content should be.
I'm fairly new to Nextjs and am probably making a silly mistake here, if anyone could point this out I'd really appreciate the help.
My mp4 video is stored within public/assets as so here:
This is my index.js file:
import Head from 'next/head';
import Image from 'next/image';
import React, { useRef, useEffect, useState } from 'react';
import ScrollAnimation from '../components/ScrollAnimation';
import Footer from '../components/Footer';
import Header from '../components/Header';
export default function Home() {
return (
<div>
<Head>
<title>Greystone</title>
<link rel='icon' href='/favicon.ico' />
<link
rel='preload'
href='/fonts/FairplayDisplay/FairplayDisplay-Bold.ttf'
as='font'
crossOrigin=''
/>
<link
rel='preload'
href='/fonts/FairplayDisplay/FairplayDisplay-Regular.ttf'
as='font'
crossOrigin=''
/>
</Head>
<main className='overflow-y-hidden h-screen'>
<Header />
<div
id='main'
className='transition duration-1000 relative ease-in-out'
onWheel={scrollTo}>
{/* Page 1 */}
<div className='h-screen w-full flex items-center'>
<video autoplay loop muted className='w-full h-screen z-10'>
<source
src='../public/assets/bubble-video.mp4'
type='video/mp4'
/>
</video>
<div className='flex flex-col absolute right-20'>
<ScrollAnimation />
</div>
<div className='w-2/5 text-left flex flex-col text-white left-20 absolute'>
<h2 className='text-5xl'>We’re Greystone.</h2>
<h2 className='text-5xl'>
We think recruitment is broken. Be part of something better.
</h2>
<p className='text-left mt-5 text-lg'>
Greystone brings the top 10% of talent together in an exclusive
club, then pairs those dream hires with the right clients. It’s
role-finding, reimagined.
</p>
<div className='mt-5 border-b-2 border-white w-36'>
<h3 className='text-left text-lg'>FIND OUT MORE</h3>
</div>
</div>
</div>
</div>
<Footer />
</main>
</div>
);
}
Many thanks in advance if anyone can help!
It seems I didn't need to have 'public' in the src url call for anyone who may experience a similar issue in future. The video tag now looks like so:
<video autoPlay loop muted className='w-full h-screen z-10'>
<source src='/assets/bubble-video.mp4' type='video/mp4' />
</video>
adding mute attribute to the video tag solved a similar issue for me
<video
preload="auto"
playsInline
autoPlay
muted
loop
>
<source src="/video.mp4" type="video/mp4" />
</video>

Why is video player working here and not there

I made a Sandbox showing the problem. In the Sandbox the Video player is working but replacing the code with below code is not working. If I move the src={filePa.. to <video.. then it works but without caption dunno
I write this code and it works:
return (
<div className="pg-driver-view">
<div className="video-container">
{this.renderLoading()}
<video
style={{ visibility }}
controls
type={`video/${fileType}`}
onCanPlay={e => this.onCanPlay(e)}
src={filePath}
>
Video playback is not supported by your browser.
</video>
</div>
</div>
);
Then I try this and it's not working the player does not show no warning no messages in the log:
return (
<div className="pg-driver-view">
<div className="video-container">
{this.renderLoading()}
<video style={{ visibility }} controls onCanPlay={e => this.onCanPlay(e)}>
Video playback is not supported by your browser.
<source src={filePath} type={`video/${fileType}`} />
<track src="test_captions_en.vtt" kind="captions" srcLang="en" label="hello_english_captions" />
</video>
</div>
</div>
);
Any idea?

Autoplay is removed from HTML after rendering

I am implementing HTML in react to generate and facing a problem with video autoplay
this is how my file looks like
home.js
import video_two from './../images/tab-sidebar.mp4';
class Noticias extends Component {
render() {
return(
<video autoplay muted loop class="hide-tablet video-2">
<source src={video_two} type="video/mp4" />
<p>Joykal Infotech</p>
</video>
)
}
}
generated HTML
<video loop="" class="hide-tablet">
<source src="/static/media/noticias-full-website-scroll.069654da.webm" type="video/webm">
<p>Joykal Infotech</p>
</video>
I don't understand why and how the autoplay and muted are removed from the video tag. Suggestions are appreciated
Update
Figured it myself the react is camelcase sensitive and it supports autoPlay rather than autoplay.
React is camelcase sensitive so you should use autoPlay instead of autoplay
like this:
<video autoPlay muted loop class="hide-tablet video-2">
<source src={video_two} type="video/mp4" />
<p>Joykal Infotech</p>
</video>

Video autoplay not working - Trying to find a fix

I have a video background in React. I want it to autoplay. Autoplay does not work.
I have tried:
autoplay
autoPlay
autoPlay="autoplay"
What I find particularly odd is that occasionally, it WILL work. But then it will stop.
Here is the code as it is right now.
<video loop autoPlay>
<source src={require('../../videos/background.mp4')} type="video/mp4" />
</video>
Here is the entire section of the component. I'm using a transition, but as of last week, it didn't impact it.
<div className="video-background">
<Transition in={true} timeout={1000} appear={true}>
{(state) => (
<div id="banner" className="video-foreground" style={{
...transitionStyles[state]
}}>
<video loop autoPlay>
<source src={require('../../videos/background.mp4')} type="video/mp4" />
</video>
</div>
)}
</Transition>
</div>
Well in my case added muted property.
Try this:
<video ... autoPlay muted />
Chrome 66 stops autoplay of all video that don't have the muted property.
In my case the video in React did not play because I didn't capitalize autoplay. It has to be autoPlay.
Try this (it works): autoPlay={true}
Try <video loop muted autoPlay controls = ''> ... </video>
Apparently, when placing the controls, it is possible to play the video, then placing the controls = '', we can remove the buttons from the controls and autoPlay works again.
I used useRef and the problem of not playing the video was solved.
const vidRef=useRef();
useEffect(() => { vidRef.current.play(); },[]);
<video
src="/videos.mp4"
ref={ vidRef }
muted
autoPlay
loop
/>
autoplay work with muted & controls
<video controls muted autoPlay >
<source type="video/webm" src=".../.webm" />
</video>
Well..
You can try this
autoplay=""
Like this
<video autoplay="">
<source src={require('../../videos/background.mp4')} type="video/mp4" />
</video>
if you are facing this issue ,try this it will work 100%
<video
src="/videos/ab.mp4"
controls
muted
autoPlay={"autoplay"}
preLoad="auto"
loop
> something</video>
This code gives you the solution
<video
src="/videos.mp4"
controls
muted
autoPlay={"autoplay"}
preLoad="auto"
loop
> </video>
Adding somethigs in the video body it works in my case.
<video
src="/assets/videos/presentation.mp4"
muted
autoPlay
loop >
My brand like image alt
</video>
I think the order in which you have it makes a difference too. I had autoPlay loop muted control and the video only worked on the computer with the control. Once I did muted autoPlay the video then played played on it's own as well as on mobile!
import video from 'src/../../video'
<video muted autoPlay loop>
<source src={video} />
</video>
It works only when the video is muted so please add muted attribute
<div>
<video src="/assets/loading.mp4" loop autoPlay muted className="h-[300px] w-auto" ></video>
</div>
Try allow="autoplay"

Resources