Dragable p is a relatively difficult effect to achieve, especially when the browser is not efficient enough for js code. However, I heard that Firefox’s support for js is increasing, which is probably the case. Come to the important position js plays in the desktop trend of web browsing.
To achieve arbitrary dragging of p, we might as well analyze the whole process.
When the mouse clicks p, an event is triggered so that the position attributes (left, top) of p change with the mouse position. When the mouse is released, the position attribute of p uses the position when the mouse is released.
It is easy to trigger an event when the mouse clicks. Just add onmouseclick to the p tag. Now the problem we have to solve is how to make the position of p change with the position of the mouse.
Although this may be a very simple reasoning process, let’s make it clear. The left and top of p are the coordinates of the upper left corner of p. When we move the mouse to p and click, there is no doubt that the coordinates of the mouse and the coordinates of p are inconsistent. At this time, if we simply make the coordinates of p equal to the coordinates of the mouse , then the effect will not look so perfect, so we first need to get the difference between the mouse coordinates and the p coordinate, and then when the mouse moves to it, subtract this difference from the mouse coordinates to get the p coordinate (if not If you understand it too well, then learn the basics of web pages first).
The next thing is simple. When the mouse moves, we continuously calculate the coordinate of p and change it. When the mouse is released, this event is removed.
The entire js function is as follows:
function beginDrag(elementToDrag,event)
{
var deltaX=event.clientX-parseInt(elementToDrag.style.left);
var deltaY=event.clientY -parseInt(elementToDrag.style.top);
if(document.addEventListener)
{
document.addEventListener("mousemove",moveHandler,true);
document.addEventListener("mouseup", upHandler,true);
//document.addEventListener("mouseout",upHandler,true);
}
else if(document.attachEvent)
{
document.attachEvent("onmousemove ",moveHandler);
document.attachEvent("onmouseup",upHandler);
//document.attachEvent("onmouseout",upHandler);
}
if(event.stopPropagation) event. stopPropagation();
else event.cancelBubble=true;
if(event.preventDefault) event.preventDefault();
else event.returnValue=false;
function moveHandler(e)
{
if (!e) e=window.event; //If it is an IE event object, then use window.event
//Global attributes, otherwise use the DOM secondary standard Event object.
elementToDrag.style.left=(e.clientX-deltaX) ”px”;
elementToDrag.style.top=(e.clientY-deltaY) ”px”;
if(e.stopPropagation) e .stopPropagation();
else e.cancelBubble=true;
}
function upHandler(e)
{
if(document.removeEventListener)
{
document.removeEventListener ("mouseup",upHandler,true);
document.removeEventListener("mousemove",moveHandler,true);}
else
{
document.detachEvent("onmouseup",upHandler);
document.detachEvent("onmousemove",moveHandler);}
}
if (!e) e=window.event;
if(e.stopPropagation) e.stopPropagation();
else e.cancelBubble=true;
}
About this js function that implements p dragging, it was actually published on the Internet by a senior. Here is just an excerpt and annotation.
function beginDrag(elementToDrag,event)
{
var =event.clientX-parseInt(elementToDrag.style.left);
var deltaY=event.clientY-parseInt(elementToDrag.style.top) ;
//The deltaX/Y here is actually the coordinate difference between the mouse and p.
if(document.addEventListener)
//The reason why such a judgment is added here is because IE6 and firefox have different methods for JavaScript event processing (versions after IE7 begin to comply with W3C standards).
//If document.addEventlistener is true, it is a browser such as Firefox that supports the W3C DOM standard. In IE6, attachEvent is used to register events, while browsers such as Firefox use addEventListener. The syntax is as follows. The true parameter of the addEventListener function indicates that the event can be captured.
{
document.addEventListener("mousemove",moveHandler,true);
document.addEventListener("mouseup",upHandler,true);
//document.addEventListener("mouseout",upHandler ,true);
}
else if(document.attachEvent)
{
document.attachEvent("onmousemove",moveHandler);
document.attachEvent("onmouseup",upHandler);
//document.attachEvent("onmouseout",upHandler);
}
if(event.stopPropagation) event.stopPropagation();
else event.cancelBubble=true;
//여기서의 판단은 여전히 다양한 브라우저를 고려합니다. stopPropagation은 W3C DOM 표준에서 이벤트 전파를 취소하는 데 사용되는 방법입니다. document.addEventListener 메소드를 사용했습니다. 브라우저는 DOM 노드를 따라 대상 노드로 전파되고, 상위 노드에도 이벤트가 반환됩니다. 해당 이벤트 핸들러를 사용하면 이벤트도 처리됩니다. 이러한 상황을 방지하기 위해 stopPropagation을 사용하여 이벤트 전파를 방지할 수 있습니다. 이 메소드의 기능은 이 이벤트에 다른 요소를 표시하는 것입니다. IE6에는 요소가 이벤트를 캡처하는 프로세스가 없지만 버블 프로세스라는 용어가 있습니다. IE6에서 사용되는 메소드는 버블을 취소하는 데 사용되며 이벤트가 처리되었으며 다른 요소가 더 이상 필요하지 않음을 나타냅니다. 볼 수 있습니다.
if(event.preventDefault) event.preventDefault();
else event.returnValue=false;
//여기서 PreventDefault는 이벤트와 관련된 기본 작업을 수행하지 않도록 브라우저에 알리는 데 사용됩니다. returnValue는 이벤트가 발생한 소스 요소의 기본 동작을 취소하는 데 사용됩니다. 이는 다른 브라우저에서도 동일한 역할을 하는 것을 볼 수 있습니다.
//다음은 p 드래그에 사용되는 주요 기능입니다.
function moveHandler(e)
{
if (!e) e=window.event; //IE 이벤트 객체라면 window.event를 사용하세요.
//그렇지 않으면 전역 속성을 사용하세요. DOM 2차 표준 이벤트 객체를 사용하세요.
//IE에서 event는 전역변수인 window의 속성이지만, W3C DOM에서는 event가 이벤트가 발생한 문서 객체의 속성이다. 이 프로그램에서는 이벤트가 무엇인지는 중요하지 않습니다. IE에서는 e 매개변수가 전달되면 IE가 이를 인식할 수 없으므로 e를 할당합니다. window.event.
elementToDrag.style.left=(e.clientX-deltaX) ”px”;
elementToDrag.style.top=(e.clientY-deltaY) ”px”
//변경 사항은 다음과 같습니다. p의 left 및 top 속성이 사용됩니다.
if(e.stopPropagation) e.stopPropagation();
else e.cancelBubble=true;
}
function upHandler(e)
{
if(document.removeEventListener)
{
document.removeEventListener("mouseup",upHandler,true)
document.removeEventListener("mousemove",moveHandler,true)
else
{
document.detachEvent("onmouseup",upHandler);
document.detachEvent("onmousemove",moveHandler);
}
//이 함수는 비교적 간단합니다. 자세히 설명하지 않겠습니다.
if (!e) e=window.event;
if(e.stopPropagation) e.stopPropagation()
else e.cancelBubble=true;

JavaScript如何实现图片的左右拖动切换效果?在现代网页设计中,动态效果可以增加用户体验和视觉吸引力。而图片的左右拖动切换效果是一种常见的动态效果,它可以让用户通过拖动图片来切换不同的内容。在本文中,我们将介绍如何使用JavaScript来实现这种图片切换效果,并提供具体的代码示例。首先,我们需要准备一些HTML和CSS代码,用于创建一个包含多个图片

JavaScript如何实现弹出框的拖动的同时限制在页面可见区域内?在网页开发中,我们常常会遇到需要实现弹出框或对话框的需求。而其中一个常见的需求就是让这些弹出框能够随意拖动,并且限制在页面的可见区域内。本文将介绍如何使用JavaScript来实现这个功能,并提供相应的代码示例。首先,我们需要了解一些基本概念。在Web开发中,页面的可见区域可以用窗口的宽度

css实现div缺一个角的方法:1、创建一个HTML示例文件,并定义一个div;2、给div设置宽高背景色;3、给需要删除一角的div增加一个伪类,将伪类设置成跟背景色一样的颜色,然后旋转45度,再定位到需要去除的那个角即可。

JavaScript如何实现图片的拖动缩放同时限制在容器内?在Web开发中,经常会遇到需要对图片进行拖动和缩放的需求。这篇文章将介绍如何使用JavaScript实现图片的拖动缩放,并限制在容器内的操作。一、拖动图片要实现图片的拖动,我们可以使用鼠标事件来跟踪鼠标位置,并将图片的位置随之移动。下面是一个示例代码://获取图片元素varimage

前言最近GitHub上有个基于ChatGPTAPI的浏览器脚本,openai-translator,短时间内star冲到了12k,功能上除了支持翻译外,还支持润色和总结功能,除了浏览器插件外,还使用了tauri打包了一个桌面客户端,那抛开tauri是使用rust部分,那浏览器部分实现还是比较简单的,今天我们就来手动实现一下。openAI提供的接口比如我们可以复制以下代码,在浏览器控制台中发起请求,就可以完成翻译//示例constOPENAI_API_KEY="s

JavaScript如何实现图片的拖动缩放同时限制在容器内且保持纵横比和居中显示?在现代web开发中,图片的拖动、缩放和限制在容器内是非常常见的需求,今天我们将学习如何使用JavaScript实现这个功能,并且保持图片的纵横比和居中显示。首先,我们需要一个HTML页面来展示图片和容器。请确保在HTML文档中引入一个用于显示图片的HTML

JavaScript如何实现图片的上下拖动切换效果?随着互联网的发展,图片在我们生活和工作中扮演着重要的角色。为了提升用户体验,我们常常需要给图片增加一些特效或交互效果。其中,图片的上下拖动切换效果是一种很常见、简洁且实用的效果。本文将介绍如何使用JavaScript实现这一效果,并提供具体的代码示例。首先,我们需要创建一个HTML文件,来展示图片并实现拖

区别有:1、div是一个块级元素,span是一个行内元素;2、div会自动占据一行,span则不会自动换行;3、div用于包裹比较大的结构和布局,span用于包裹文本或者其他行内元素;4、div可以包含其他块级元素和行内元素,span可以包含其他行内元素。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
