- “This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”
preface
- Doing front-end development for so long, you’re right
JavaScript
How much do you know about native events? How is it possible to play video in full/out of full screen, drag and drop files to upload, adjust the width and height of containers, adjust the left and right container widths, and so on? Below I do a simple introduction for the above several functions, shortcomings also hope to point out. - Detanx Github
Full screen/Exit full screen
- In some cases, when we want to see only one part of the page due to a large amount of content, we can add a full-screen button to that part of the page to show only that part of the content.
- Note: Full screen events are in
document.documentElement
Object, while exiting the full screen event is indocument
On the object.
- When setting the style in different states (not limited to width and height), we can modify the different
class
To control, inVue
、React
Etc. We can also control variables to change the elements in different statesclass
。 - How to remember these event names:
- Full screen event
Source:document.documentElement
;
Method name: Standard method -:requestFullscreen
, other methods -: capital standard methodrequestFullscreen
Is prefixed with the first letter ofwebkit
,moz
,ms
). - Exit the full-screen event
Source:document
;
Method name: Standard method -:exitFullscreen
.IE
-: uppercase standard methodexitFullscreen
The first letter of “and prefixedms
;Chrome (its)
,Firefox (moz)
– :CancelFullScreen
Add the corresponding kernel prefix.
- Full screen event
File drag upload
- File uploads are used in many places, half of them by clicking on a button or area to select file uploads, but other places use drag-and-drop file uploads, or both.
Drag and drop event
- In file drag-and-drop, we’re going to use that
ondragover
,ondragenter
,ondragleave
,ondrop
These events.
The specific implementation
- Create a drag-and-drop area to accept files and a progress bar to read files
2. Listen for the drag event on the web page and display the corresponding file to be dragged to the area, if the drag area is always displayed, you can directly tie the event to the corresponding area element.
3. Triggered when a drag file has entered the drag areaondragenter
.4. Triggered when a drag file leaves the drag areaondragleave
.5. Triggered when a drag file is released in the drag areaondrop
Event, when the file being dragged is not some unreadable content (e.g. folder, etc.), we can use the built-inFileReader
Object processing, passFileReader
Instance of some methods, we can do some of our own processing.
Drag and drop to changediv
The size of the
- In some projects, there may be a requirement that the page has a container and we can change the height of the container by holding down the edge and dragging the mouse
textarea
Of the labelresize
Attribute); Of course we certainly have more than this simple function when the page exists left and rightdiv
When, we can drag twodiv
In the middle, make twodiv
One increases the width of the other decreases, but always keeps the total width the same.
Create a container
Listen for mouse movement events
- Get the position of the mouse, determine whether it is inside the container, determine the location of the container when it is inside.
resizeable
: Whether the mouse-down area is defined to change the size of the container;clientX
,clientY
: record the coordinates of the mouse, also used to store the coordinates of the mouse last time;minW
,minH
,direc
: Minimum width, height and change direction of the container.
CSS
的cursor
Property allows you to set the mouse to be displayed at different locations of the container, informing the user of the actions that can be performed by the container.
- More CURSOR properties.
Listen for mouse press events
- Mouse-down events should be added to containers to avoid triggering events in non-container areas.
Listen for mouse release events
- The release event should be bound to the entire document object. If it is bound to the container, when the mouse is pressed over the container, we will move the mouse out of the container, and the release event will not occur.
A two-column mutable container
- through
flex
Layout, we add a two-column, and set a region in the middle where you can click to change the width of the container on both sides of the version. After pressing the region with the mouse, change the width of the left and right containers by obtaining the position of the mouse. - The concrete implementation is as follows:
- Create the required container structure
- through
CSS
Adjust the layout accordingly
- Add drag events to resize
- Depending on where the container is, the variable inside the container
div
The size calculation may also be somewhat different, for example, if the container existspadding
Phi, and then you have to subtract the corresponding phipx
Size and so on. - The mouse may move out of the click-drag area during drag, so the release event (
onmouseup
) need to bind todocument
On.
Past wonderful
- JavaScript judges elegant writing
- Build from 0 Vite + Vue3 + element-plus + VUE-Router + ESLint + husky + Lint-staged
- “Front-end advanced” JavaScript handwriting methods/use tips self-check
- An introduction to JavaScript design patterns and creative patterns
- Public account open small program best solution (Vue)
- Axios you probably don’t know how to use
“Likes, favorites and comments”
❤️ follow + like + favorites + comments + forward ❤️, creation is not easy, encourage the author to create a better article, thank 🙏 everyone.