1592076660
Vue directive for conditional rendering (like v-if) element on screen smaller than breakpoints;
$ npm install --save vue-not-visible
$ yarn add vue-not-visible
import Vue from 'vue'
import vueNotVisible from 'vue-not-visible'
/* const BREAKPOINTS = {
mobile: 425,
tablet: 768,
tablet_landscape: 1024,
desktop: 1200,
desktop_large: 1440,
hd: 1920,
}
*/
Vue.use(vueNotVisible) // this is default
<template>
<div id="test">
{{ message }} {{ count }}
<div v-not-visible="'tablet'">
<div v-on:click="count = count + 1">Not visible on table(screen < 768)</div>
</div>
<div v-not-visible="'mobile'">
<div v-on:click="count = count + 1">Not visible on mobile(screen < 425)</div>
</div>
</div>
</template>
import Vue from 'vue'
import vueNotVisible from 'vue-not-visible'
Vue.use(vueNotVisible, {ipad: 1280}) // this is custom
<template>
<div id="test">
{{ message }} {{ count }}
<div v-not-visible="'ipad'">
<div v-on:click="count = count + 1">Not visible on ipad(screen < 1280)</div>
</div>
</div>
</template>
Author: PxyUp
Live Demo: https://pxyup.github.io/vue-not-visible/index.html
GitHub: https://github.com/PxyUp/vue-not-visible
#vuejs #javascript #vue #vue-js
1592076660
Vue directive for conditional rendering (like v-if) element on screen smaller than breakpoints;
$ npm install --save vue-not-visible
$ yarn add vue-not-visible
import Vue from 'vue'
import vueNotVisible from 'vue-not-visible'
/* const BREAKPOINTS = {
mobile: 425,
tablet: 768,
tablet_landscape: 1024,
desktop: 1200,
desktop_large: 1440,
hd: 1920,
}
*/
Vue.use(vueNotVisible) // this is default
<template>
<div id="test">
{{ message }} {{ count }}
<div v-not-visible="'tablet'">
<div v-on:click="count = count + 1">Not visible on table(screen < 768)</div>
</div>
<div v-not-visible="'mobile'">
<div v-on:click="count = count + 1">Not visible on mobile(screen < 425)</div>
</div>
</div>
</template>
import Vue from 'vue'
import vueNotVisible from 'vue-not-visible'
Vue.use(vueNotVisible, {ipad: 1280}) // this is custom
<template>
<div id="test">
{{ message }} {{ count }}
<div v-not-visible="'ipad'">
<div v-on:click="count = count + 1">Not visible on ipad(screen < 1280)</div>
</div>
</div>
</template>
Author: PxyUp
Live Demo: https://pxyup.github.io/vue-not-visible/index.html
GitHub: https://github.com/PxyUp/vue-not-visible
#vuejs #javascript #vue #vue-js
1600583123
In this article, we are going to list out the most popular websites using Vue JS as their frontend framework.
Vue JS is one of those elite progressive JavaScript frameworks that has huge demand in the web development industry. Many popular websites are developed using Vue in their frontend development because of its imperative features.
This framework was created by Evan You and still it is maintained by his private team members. Vue is of course an open-source framework which is based on MVVM concept (Model-view view-Model) and used extensively in building sublime user-interfaces and also considered a prime choice for developing single-page heavy applications.
Released in February 2014, Vue JS has gained 64,828 stars on Github, making it very popular in recent times.
Evan used Angular JS on many operations while working for Google and integrated many features in Vue to cover the flaws of Angular.
“I figured, what if I could just extract the part that I really liked about Angular and build something really lightweight." - Evan You
#vuejs #vue #vue-with-laravel #vue-top-story #vue-3 #build-vue-frontend #vue-in-laravel #vue.js
1659694200
public_activity
provides easy activity tracking for your ActiveRecord, Mongoid 3 and MongoMapper models in Rails 3 and 4.
Simply put: it can record what happens in your application and gives you the ability to present those recorded activities to users - in a similar way to how GitHub does it.
You probably don't want to read the docs for this unreleased version 2.0.
For the stable 1.5.X
readme see: https://github.com/chaps-io/public_activity/blob/1-5-stable/README.md
Here is a simple example showing what this gem is about:
Ryan Bates made a great screencast describing how to integrate Public Activity.
A great step-by-step guide on implementing activity feeds using public_activity by Ilya Bodrov.
You can see an actual application using this gem here: http://public-activity-example.herokuapp.com/feed
The source code of the demo is hosted here: https://github.com/pokonski/activity_blog
You can install public_activity
as you would any other gem:
gem install public_activity
or in your Gemfile:
gem 'public_activity'
By default public_activity
uses Active Record. If you want to use Mongoid or MongoMapper as your backend, create an initializer file in your Rails application with the corresponding code inside:
For Mongoid:
# config/initializers/public_activity.rb
PublicActivity.configure do |config|
config.orm = :mongoid
end
For MongoMapper:
# config/initializers/public_activity.rb
PublicActivity.configure do |config|
config.orm = :mongo_mapper
end
(ActiveRecord only) Create migration for activities and migrate the database (in your Rails project):
rails g public_activity:migration
rake db:migrate
Include PublicActivity::Model
and add tracked
to the model you want to keep track of:
For ActiveRecord:
class Article < ActiveRecord::Base
include PublicActivity::Model
tracked
end
For Mongoid:
class Article
include Mongoid::Document
include PublicActivity::Model
tracked
end
For MongoMapper:
class Article
include MongoMapper::Document
include PublicActivity::Model
tracked
end
And now, by default create/update/destroy activities are recorded in activities table. This is all you need to start recording activities for basic CRUD actions.
Optional: If you don't need #tracked
but still want the comfort of #create_activity
, you can include only the lightweight Common
module instead of Model
.
You can trigger custom activities by setting all your required parameters and triggering create_activity
on the tracked model, like this:
@article.create_activity key: 'article.commented_on', owner: current_user
See this entry http://rubydoc.info/gems/public_activity/PublicActivity/Common:create_activity for more details.
To display them you simply query the PublicActivity::Activity
model:
# notifications_controller.rb
def index
@activities = PublicActivity::Activity.all
end
And in your views:
<%= render_activities(@activities) %>
Note: render_activities
is an alias for render_activity
and does the same.
You can also pass options to both activity#render
and #render_activity
methods, which are passed deeper to the internally used render_partial
method. A useful example would be to render activities wrapped in layout, which shares common elements of an activity, like a timestamp, owner's avatar etc:
<%= render_activities(@activities, layout: :activity) %>
The activity will be wrapped with the app/views/layouts/_activity.html.erb
layout, in the above example.
Important: please note that layouts for activities are also partials. Hence the _
prefix.
Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
<%= render_activity(@activity, locals: {friends: current_user.friends}) %>
Note: Before 1.4.0, one could pass variables directly to the options hash for #render_activity
and access it from activity parameters. This functionality is retained in 1.4.0 and later, but the :locals
method is preferred, since it prevents bugs from shadowing variables from activity parameters in the database.
public_activity
looks for views in app/views/public_activity
.
For example, if you have an activity with :key
set to "activity.user.changed_avatar"
, the gem will look for a partial in app/views/public_activity/user/_changed_avatar.html.(|erb|haml|slim|something_else)
.
Hint: the "activity."
prefix in :key
is completely optional and kept for backwards compatibility, you can skip it in new projects.
If you would like to fallback to a partial, you can utilize the fallback
parameter to specify the path of a partial to use when one is missing:
<%= render_activity(@activity, fallback: 'default') %>
When used in this manner, if a partial with the specified :key
cannot be located it will use the partial defined in the fallback
instead. In the example above this would resolve to public_activity/_default.html.(|erb|haml|slim|something_else)
.
If a view file does not exist then ActionView::MisingTemplate will be raised. If you wish to fallback to the old behaviour and use an i18n based translation in this situation you can specify a :fallback
parameter of text
to fallback to this mechanism like such:
<%= render_activity(@activity, fallback: :text) %>
Translations are used by the #text
method, to which you can pass additional options in form of a hash. #render
method uses translations when view templates have not been provided. You can render pure i18n strings by passing {display: :i18n}
to #render_activity
or #render
.
Translations should be put in your locale .yml
files. To render pure strings from I18n Example structure:
activity:
article:
create: 'Article has been created'
update: 'Someone has edited the article'
destroy: 'Some user removed an article!'
This structure is valid for activities with keys "activity.article.create"
or "article.create"
. As mentioned before, "activity."
part of the key is optional.
For RSpec you can first disable public_activity
and add require helper methods in the rails_helper.rb
with:
#rails_helper.rb
require 'public_activity/testing'
PublicActivity.enabled = false
In your specs you can then blockwise decide whether to turn public_activity
on or off.
# file_spec.rb
PublicActivity.with_tracking do
# your test code goes here
end
PublicActivity.without_tracking do
# your test code goes here
end
For more documentation go here
You can set up a default value for :owner
by doing this:
PublicActivity::StoreController
in your ApplicationController
like this:class ApplicationController < ActionController::Base
include PublicActivity::StoreController
end
:owner
attribute for tracked
class method in your desired model. For example:class Article < ActiveRecord::Base
tracked owner: Proc.new{ |controller, model| controller.current_user }
end
Note: current_user
applies to Devise, if you are using a different authentication gem or your own code, change the current_user
to a method you use.
If you need to disable tracking temporarily, for example in tests or db/seeds.rb
then you can use PublicActivity.enabled=
attribute like below:
# Disable p_a globally
PublicActivity.enabled = false
# Perform some operations that would normally be tracked by p_a:
Article.create(title: 'New article')
# Switch it back on
PublicActivity.enabled = true
You can also disable public_activity for a specific class:
# Disable p_a for Article class
Article.public_activity_off
# p_a will not do anything here:
@article = Article.create(title: 'New article')
# But will be enabled for other classes:
# (creation of the comment will be recorded if you are tracking the Comment class)
@article.comments.create(body: 'some comment!')
# Enable it again for Article:
Article.public_activity_on
Besides standard, automatic activities created on CRUD actions on your model (deactivatable), you can post your own activities that can be triggered without modifying the tracked model. There are a few ways to do this, as PublicActivity gives three tiers of options to be set.
Because every activity needs a key (otherwise: NoKeyProvided
is raised), the shortest and minimal way to post an activity is:
@user.create_activity :mood_changed
# the key of the action will be user.mood_changed
@user.create_activity action: :mood_changed # this is exactly the same as above
Besides assigning your key (which is obvious from the code), it will take global options from User class (given in #tracked
method during class definition) and overwrite them with instance options (set on @user
by #activity
method). You can read more about options and how PublicActivity inherits them for you here.
Note the action parameter builds the key like this: "#{model_name}.#{action}"
. You can read further on options for #create_activity
here.
To provide more options, you can do:
@user.create_activity action: 'poke', parameters: {reason: 'bored'}, recipient: @friend, owner: current_user
In this example, we have provided all the things we could for a standard Activity.
Besides the few fields that every Activity has (key
, owner
, recipient
, trackable
, parameters
), you can also set custom fields. This could be very beneficial, as parameters
are a serialized hash, which cannot be queried easily from the database. That being said, use custom fields when you know that you will set them very often and search by them (don't forget database indexes :) ).
owner
and recipient
based on associationsclass Comment < ActiveRecord::Base
include PublicActivity::Model
tracked owner: :commenter, recipient: :commentee
belongs_to :commenter, :class_name => "User"
belongs_to :commentee, :class_name => "User"
end
class Post < ActiveRecord::Base
include PublicActivity::Model
tracked only: [:update], parameters: :tracked_values
def tracked_values
{}.tap do |hash|
hash[:tags] = tags if tags_changed?
end
end
end
Skip this step if you are using ActiveRecord in Rails 4 or Mongoid
The first step is similar in every ORM available (except mongoid):
PublicActivity::Activity.class_eval do
attr_accessible :custom_field
end
place this code under config/initializers/public_activity.rb
, you have to create it first.
To be able to assign to that field, we need to move it to the mass assignment sanitizer's whitelist.
If you're using ActiveRecord, you will also need to provide a migration to add the actual field to the Activity
. Taken from our tests:
class AddCustomFieldToActivities < ActiveRecord::Migration
def change
change_table :activities do |t|
t.string :custom_field
end
end
end
Assigning is done by the same methods that you use for normal parameters: #tracked
, #create_activity
. You can just pass the name of your custom variable and assign its value. Even better, you can pass it to #tracked
to tell us how to harvest your data for custom fields so we can do that for you.
class Article < ActiveRecord::Base
include PublicActivity::Model
tracked custom_field: proc {|controller, model| controller.some_helper }
end
If you need help with using public_activity please visit our discussion group and ask a question there:
https://groups.google.com/forum/?fromgroups#!forum/public-activity
Please do not ask general questions in the Github Issues.
Author: public-activity
Source code: https://github.com/public-activity/public_activity
License: MIT license
1598685221
In this tutorial, I will show you how to upload a file in Vue using vue-dropzone library. For this example, I am using Vue.js 3.0. First, we will install the Vue.js using Vue CLI, and then we install the vue-dropzone library. Then configure it, and we are ready to accept the file. DropzoneJS is an open source library that provides drag and drops file uploads with image previews. DropzoneJS is lightweight doesn’t depend on any other library (like jQuery) and is highly customizable. The vue-dropzone is a vue component implemented on top of Dropzone.js. Let us start Vue File Upload Using vue-dropzone Tutorial.
Dropzone.js is an open-source library providing drag-and-drop file uploads with image previews. DropzoneJS is lightweight, doesn’t depend on any other library (like jQuery), and is highly customizable.
The vue-dropzone is a vue component implemented on top of Dropzone.js.
First, install the Vue using Vue CLI.
Go to your terminal and hit the following command.
npm install -g @vue/cli
or
yarn global add @vue/cli
If you face any error, try running the command as an administrator.
Now, we need to generate the necessary scaffold. So type the following command.
vue create vuedropzone
It will install the scaffold.
Open the project in your favorite editor. Mine is Visual Studio Code.
cd vuedropzone
code .
I am using the Yarn package manager. So let’s install using Yarn. You can use NPM, also. It does not matter.
yarn add vue2-dropzone
or
npm install vue2-dropzone
Okay, now we need to add one css file with the above package. Now, vue cli uses css loader, so we can directly import in the src >> main.js entry file.
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App)
}).$mount('#app')
import 'vue2-dropzone/dist/vue2Dropzone.css'
If importing css is not working for you, then you need to install that CSS file manually.
Copy this vue2Dropzone.css file’s content.
Create one file inside the src >> assets folder, create one css file called vuedropzone.css and paste the content there.
Import this css file inside src >> App.vue file.
<style lang="css">
@import './assets/vuedropzone.css';
</style>
Now, it should include in our application.
Our primary boilerplate has one ready-made component called HelloWorld.vue inside src >> components folder. Now, create one more file called FileUpload.vue.
Add the following code to FileUpload.vue file.
// FileUpload.vue
<template>
<div id="app">
<vue-dropzone id="upload" :options="config"></vue-dropzone>
</div>
</template>
<script>
import vueDropzone from "vue2-dropzone";
export default {
data: () => ({
config: {
url: "https://appdividend.com"
}
}),
components: {
vueDropzone
}
};
</script>
Here, our API endpoint is https://appdividend.com. It is the point where we will hit the POST route and store our image, but it is my blog’s homepage, so it will not work anyway. But let me import this file into App.vue component and see what happens.
// App.vue
<template>
<div id="app">
<FileUpload />
</div>
</template>
<script>
import FileUpload from './components/FileUpload.vue'
export default {
name: 'app',
components: {
FileUpload
}
}
</script>
<style lang="css">
@import './assets/vuedropzone.css';
</style>
Now, start the development server using the following command. It will open up URL: http://localhost:8080.
npm run serve
Now, after uploading the image, we can see that the image upload is failed due to the wrong POST request endpoint.
Install the Laravel.
After that, we configure the database in the .env file and use MySQL database.
We need to create one model and migration file to store the image. So let us install the following command inside the Laravel project.
php artisan make:model Image -m
It will create both the Image model and create_images_table.php migrations file.
Now, open the migrations file and add the schema to it.
// create_images_table.php
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('image_name');
$table->timestamps();
});
}
Now, migrate the database table using the following command.
php artisan migrate
It creates the table in the database.
Now, we need to add a laravel-cors package to prevent cross-site-allow-origin errors. Go to the Laravel root and enter the following command to install it.
composer require barryvdh/laravel-cors
Configure it in the config >> app.php file.
Barryvdh\Cors\ServiceProvider::class,
Add the middleware inside app >> Http >> Kernel.php file.
// Kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\Barryvdh\Cors\HandleCors::class,
];
First, create an ImageController.php file using the following command.
php artisan make:controller ImageController
Define the store method. Also, create one images folder inside the public directory because we will store an image inside it.
Right now, I have written the store function that handles one image at a time. So do not upload multiple photos at a time; otherwise, it will break.
// ImageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Image;
class ImageController extends Controller
{
public function store(Request $request)
{
if($request->file('file'))
{
$image = $request->file('file');
$name = time().$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
}
$image= new Image();
$image->image_name = $name;
$image->save();
return response()->json(['success' => 'You have successfully uploaded an image'], 200);
}
}
Go to the routes >> api.php file and add the following route.
// api.php
Route::post('image', 'ImageController@store');
We need to add the correct Post request API endpoint in FileUpload.vue component.
// FileUpload.vue
<template>
<div id="app">
<vue-dropzone id="drop1" :options="config" @vdropzone-complete="afterComplete"></vue-dropzone>
</div>
</template>
<script>
import vueDropzone from "vue2-dropzone";
export default {
data: () => ({
config: {
url: "http://localhost:8000/api/image",
}
}),
components: {
vueDropzone
},
methods: {
afterComplete(file) {
console.log(file);
}
}
};
</script>
Now, save the file and try to upload an image. If everything is okay, then you will be able to save the image on the Laravel web server as well as save the name in the database as well.
You can also verify on the server side by checking the database entry and the images folder in which we have saved the image.
The only required options are url, but there are many more you can use.
For example, let’s say you want:
export default {
data: () => ({
dropOptions: {
url: "https://httpbin.org/post",
maxFilesize: 5, // MB
maxFiles: 5,
chunking: true,
chunkSize: 400, // Bytes
thumbnailWidth: 100, // px
thumbnailHeight: 100,
addRemoveLinks: true
}
})
// ...
}
Happy Coding !!!
Originally published at https://appdividend.com
#vue #vue-dropzone #vue.js #dropzone.js #dropzonejs #vue cli
1657785244
In today’s tutorial, we will learn how to create a Custom Video Player. To build this project, we need HTML, CSS and Javascript.
00:00 Intro
00:05 Preview
02:58 HTML & CSS
35:26 Step 1: Create Initial References
45:46 Step 2: Implement slider() For Volume
51:33 Step 3: Detect Device Type
57:27 Step 4: Implement Functionality For Play & Pause Button
01:03:04 Step 5: Hide/ Show Playback Speed Options
01:08:47 Step 6: Function To Set Playback Speed.
01:12:59 Step 7: Function To Mute Video
01:18:24 Step 8: Function To Set Volume
01:24:55 Step 9: Function To Set Fullscreen
01:31:47 Step 10: Function To Exit Fullscreen
01:40:08 Step 11: Function To Format Current Time & Total Time
01:44:46 Step 12: Function To Update Progress & Timer
01:50:13 Step 13: Implement Click Event On Progress Bar
01:57:26 Step 14: Function On Window Load
Before we start coding let us take a look at the project folder structure. We create a project folder called – ‘Custom Video Player’. Inside this folder, we have three files. The first file is index.html which is the HTML document. Next, we have style.css which is the stylesheet. Finally, we have script.js which is the script file.
We start with the HTML code. First, copy the code below and paste it into your HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom Video Player</title>
<!-- Font Awesome Icons -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
/>
<!-- Google Fonts -->
<link
href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div class="rotate-container hide">
<div id="rotate-icon">
<i class="fa-solid fa-rotate-left"></i>
<p>Rotate for a better experience</p>
</div>
</div>
<div class="video-container" id="video-container">
<video id="my-video" preload="metadata">
<source
src="https://dl.dropbox.com/s/l90y72zm97ayzhx/my%20video.mp4?raw=1"
type="video/mp4"
/>
Your browser does not support the video tag
</video>
<div class="controls" id="controls">
<div class="progress-container flex-space">
<div id="progress-bar">
<div id="current-progress"></div>
</div>
<div class="song-timer">
<span id="current-time">00:00</span>
<span>/</span>
<span id="max-duration">00:00</span>
</div>
</div>
<div id="video-controls" class="video-controls flex-space">
<div class="container-1 flex">
<div>
<!-- Play video -->
<button id="play-btn" class="control-btn">
<i class="fa-solid fa-play"></i>
</button>
<!-- Pause video-->
<button id="pauseButton" class="control-btn hide">
<i class="fa-solid fa-pause"></i>
</button>
</div>
<!-- volume of video-->
<div id="volume" class="volume flex">
<span id="high">
<i class="fa-solid fa-volume-high"></i>
</span>
<span class="hide" id="low">
<i class="fa-solid fa-volume-low"></i>
</span>
<span class="hide" id="mute">
<i class="fa-solid fa-volume-xmark"></i>
</span>
<input
type="range"
min="0"
max="100"
value="50"
id="volume-range"
oninput="slider()"
/>
<span id="volume-num">50</span>
</div>
</div>
<div class="container-2 flex-space">
<div class="playback">
<button id="playback-speed-btn">1x</button>
<div class="playback-options hide">
<button onclick="setPlayback(0.5)">0.5</button>
<button onclick="setPlayback(1.0)">1</button>
<button onclick="setPlayback(2.0)">2</button>
</div>
</div>
<!-- screen size -->
<div id="size-screen">
<button id="screen-expand">
<i class="fa-solid fa-expand"></i>
</button>
<button id="screen-compress" class="hide">
<i class="fa-solid fa-compress"></i>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Script -->
<script src="script.js"></script>
</body>
</html>
Next, we style our video player using CSS. For this copy, the code provided to you below and paste it into your stylesheet.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
outline: none;
color: #ffffff;
font-family: "Roboto Mono", monospace;
}
body {
background-color: #2887e3;
}
.flex {
display: flex;
}
.flex-space {
display: flex;
justify-content: space-between;
}
.container {
padding: 1em 0;
}
#my-video {
width: 100%;
}
.rotate-container {
top: 0;
position: absolute;
height: 100%;
width: 100%;
background-color: rgba(0, 0, 0, 0.3);
display: flex;
justify-content: center;
align-items: center;
}
#rotate-icon {
display: flex;
flex-direction: column;
color: #dddddd;
text-align: center;
}
.hide {
display: none;
}
.video-container {
width: 60%;
position: absolute;
transform: translate(-50%, -50%);
left: 50%;
top: 50%;
box-shadow: 20px 30px 50px rgba(0, 0, 0, 0.2);
}
.controls {
position: absolute;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(35, 34, 39, 0.8);
}
.progress-container {
align-items: center;
padding: 0 0.5em;
}
.video-controls {
flex-direction: row;
align-items: center;
}
#progress-bar {
position: relative;
width: 75%;
height: 5px;
background-color: #000000;
margin: 1em 0;
vertical-align: 2px;
border-radius: 5px;
cursor: pointer;
}
.song-timer {
font-size: 0.8em;
width: 25%;
text-align: right;
}
#current-progress {
position: absolute;
left: 0;
display: inline-block;
height: 5px;
width: 0;
background: #2887e3;
border-radius: 5px;
}
#current-progress:after {
content: "";
position: absolute;
left: calc(100% - 1.5px);
top: -2.5px;
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ffffff;
}
.playback {
position: relative;
}
.control-btn,
#screen-expand,
#screen-compress {
width: 3em;
height: 3em;
outline: none;
border: none;
background-color: transparent;
}
#size-screen {
margin-left: auto;
}
.volume {
align-items: center;
margin-left: 0.6em;
}
#volume-range {
position: relative;
margin: 0 0.5em;
cursor: pointer;
height: 5px;
-webkit-appearance: none;
background-color: #000000;
border-radius: 5px;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 10px;
width: 10px;
background-color: #2887e3;
border-radius: 50%;
cursor: pointer;
border: none;
}
.fa-solid {
font-size: 1.1rem;
}
.container-2 {
width: 10%;
min-width: 70px;
align-items: center;
}
#playback-speed-btn {
position: relative;
background-color: transparent;
border: 1px solid #ffffff;
color: #ffffff;
font-size: 0.9rem;
border-radius: 5px;
padding: 0.3em 0.25em;
cursor: pointer;
}
.playback-options {
position: absolute;
bottom: 0;
background-color: #000000;
min-width: 5em;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
z-index: 1;
}
.playback-options button {
color: #ffffff;
border-left: 0;
border-right: 0;
border-top: 0;
width: 100%;
background-color: transparent;
padding: 1em;
text-decoration: none;
display: block;
}
@media all and (display-mode: fullscreen) {
.container {
padding: 0;
}
.video-container {
width: 100%;
margin: 0;
}
.controls {
position: absolute;
display: block;
bottom: 0;
left: 0;
width: 100%;
z-index: 2;
}
#progress-bar {
width: 80%;
}
.song-timer {
width: 20%;
font-size: 1.2em;
}
.fa-solid {
color: #dddddd;
}
}
@media only screen and (max-width: 768px) {
.video-container,
.controls {
width: 100%;
}
span {
display: inline;
}
#progress-bar {
width: 60%;
}
.song-timer {
width: 40%;
font-size: 0.9em;
}
.fa-solid {
font-size: 1rem;
}
.control-btn,
#screen-expand,
#screen-compress {
width: 2em;
height: 1.5em;
}
}
@media only screen and (max-width: 768px) and (display-mode: fullscreen) {
.video-container {
margin-top: 50%;
}
}
Lastly, we add functionality to our custom video player using Javascript. Once again copy the code below and paste it into your script file.
We do this in fourteen steps:
Create initial references.
Implement slider()
Detect device type.
Implement functionality for the play and pause button.
Hide/Show playback speed options
Function to set playback speed.
Logic to mute video.
Function to set Fullscreen.
Function to exit Fullscreen.
Create a function to format the current time & maximum time.
Create a function to update progress & timer.
Implement a click event on the progress bar.
Function on window load.
let videoContainer = document.querySelector(".video-container");
let container = document.querySelector(".container");
let myVideo = document.getElementById("my-video");
let rotateContainer = document.querySelector(".rotate-container");
let videoControls = document.querySelector(".controls");
let playButton = document.getElementById("play-btn");
let pauseButton = document.getElementById("pauseButton");
let volume = document.getElementById("volume");
let volumeRange = document.getElementById("volume-range");
let volumeNum = document.getElementById("volume-num");
let high = document.getElementById("high");
let low = document.getElementById("low");
let mute = document.getElementById("mute");
let sizeScreen = document.getElementById("size-screen");
let screenCompress = document.getElementById("screen-compress");
let screenExpand = document.getElementById("screen-expand");
const currentProgress = document.getElementById("current-progress");
const currentTimeRef = document.getElementById("current-time");
const maxDuration = document.getElementById("max-duration");
const progressBar = document.getElementById("progress-bar");
const playbackSpeedButton = document.getElementById("playback-speed-btn");
const playbackContainer = document.querySelector(".playback");
const playbackSpeedOptions = document.querySelector(".playback-options");
function slider() {
valPercent = (volumeRange.value / volumeRange.max) * 100;
volumeRange.style.background = `linear-gradient(to right, #2887e3 ${valPercent}%, #000000 ${valPercent}%)`;
}
//events object
let events = {
mouse: {
click: "click",
},
touch: {
click: "touchstart",
},
};
let deviceType = "";
//Detech touch device
const isTouchDevice = () => {
try {
//We try to create TouchEvent (it would fail for desktops and throw error)
document.createEvent("TouchEvent");
deviceType = "touch";
return true;
} catch (e) {
deviceType = "mouse";
return false;
}
};
//play and pause button
playButton.addEventListener("click", () => {
myVideo.play();
pauseButton.classList.remove("hide");
playButton.classList.add("hide");
});
pauseButton.addEventListener(
"click",
(pauseVideo = () => {
myVideo.pause();
pauseButton.classList.add("hide");
playButton.classList.remove("hide");
})
);
//playback
playbackContainer.addEventListener("click", () => {
playbackSpeedOptions.classList.remove("hide");
});
//if user clicks outside or on the option
window.addEventListener("click", (e) => {
if (!playbackContainer.contains(e.target)) {
playbackSpeedOptions.classList.add("hide");
} else if (playbackSpeedOptions.contains(e.target)) {
playbackSpeedOptions.classList.add("hide");
}
});
//playback speed
const setPlayback = (value) => {
playbackSpeedButton.innerText = value + "x";
myVideo.playbackRate = value;
};
//mute video
const muter = () => {
mute.classList.remove("hide");
high.classList.add("hide");
low.classList.add("hide");
myVideo.volume = 0;
volumeNum.innerHTML = 0;
volumeRange.value = 0;
slider();
};
//when user click on high and low volume then mute the audio
high.addEventListener("click", muter);
low.addEventListener("click", muter);
//for volume
volumeRange.addEventListener("input", () => {
//for converting % to decimal values since video.volume would accept decimals only
let volumeValue = volumeRange.value / 100;
myVideo.volume = volumeValue;
volumeNum.innerHTML = volumeRange.value;
//mute icon, low volume, high volume icons
if (volumeRange.value < 50) {
low.classList.remove("hide");
high.classList.add("hide");
mute.classList.add("hide");
} else if (volumeRange.value > 50) {
low.classList.add("hide");
high.classList.remove("hide");
mute.classList.add("hide");
}
});
//Screen size
screenExpand.addEventListener("click", () => {
screenCompress.classList.remove("hide");
screenExpand.classList.add("hide");
videoContainer
.requestFullscreen()
.catch((err) => alert("Your device doesn't support full screen API"));
if (isTouchDevice) {
let screenOrientation =
screen.orientation || screen.mozOrientation || screen.msOrientation;
if (screenOrientation.type == "portrait-primary") {
//update styling for fullscreen
pauseVideo();
rotateContainer.classList.remove("hide");
const myTimeout = setTimeout(() => {
rotateContainer.classList.add("hide");
}, 3000);
}
}
});
//if user presses escape the browser fire 'fullscreenchange' event
document.addEventListener("fullscreenchange", exitHandler);
document.addEventListener("webkitfullscreenchange", exitHandler);
document.addEventListener("mozfullscreenchange", exitHandler);
document.addEventListener("MSFullscreenchange", exitHandler);
function exitHandler() {
//if fullscreen is closed
if (
!document.fullscreenElement &&
!document.webkitIsFullScreen &&
!document.mozFullScreen &&
!document.msFullscreenElement
) {
normalScreen();
}
}
//back to normal screen
screenCompress.addEventListener(
"click",
(normalScreen = () => {
screenCompress.classList.add("hide");
screenExpand.classList.remove("hide");
if (document.fullscreenElement) {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
})
);
//Format time
const timeFormatter = (timeInput) => {
let minute = Math.floor(timeInput / 60);
minute = minute < 10 ? "0" + minute : minute;
let second = Math.floor(timeInput % 60);
second = second < 10 ? "0" + second : second;
return `${minute}:${second}`;
};
//Update progress every second
setInterval(() => {
currentTimeRef.innerHTML = timeFormatter(myVideo.currentTime);
currentProgress.style.width =
(myVideo.currentTime / myVideo.duration.toFixed(3)) * 100 + "%";
}, 1000);
//update timer
myVideo.addEventListener("timeupdate", () => {
currentTimeRef.innerText = timeFormatter(myVideo.currentTime);
});
//If user click on progress bar
isTouchDevice();
progressBar.addEventListener(events[deviceType].click, (event) => {
//start of progressbar
let coordStart = progressBar.getBoundingClientRect().left;
//mouse click position
let coordEnd = !isTouchDevice() ? event.clientX : event.touches[0].clientX;
let progress = (coordEnd - coordStart) / progressBar.offsetWidth;
//set width to progress
currentProgress.style.width = progress * 100 + "%";
//set time
myVideo.currentTime = progress * myVideo.duration;
//play
myVideo.play();
pauseButton.classList.remove("hide");
playButton.classList.add("hide");
});
window.onload = () => {
//display duration
myVideo.onloadedmetadata = () => {
maxDuration.innerText = timeFormatter(myVideo.duration);
};
slider();
};
That’s it for this tutorial. If you face any issues while creating this code, you can download the source code by clicking the ‘Download Code’
📁 Download Source Code : https://www.codingartistweb.com
#html #css #javascript #webdev