在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):timruffles/mobile-drag-drop开源软件地址(OpenSource Url):https://github.com/timruffles/mobile-drag-drop开源编程语言(OpenSource Language):TypeScript 86.9%开源软件介绍(OpenSource Introduction):Polyfill for HTML 5 drag'n'dropThe HTML 5 drag'n'drop API allows you to implement drag'n'drop on most desktop browsers and some mobile browsers.
See #167 for state of drag and drop in
Luckily, browsers give us enough tools to make it happen ourselves if needed. If you drop this package in your page your existing HTML 5 drag'n'drop code should just work (*almost). DemosCheck out the demo to see it in action and monitor the console to see the events firing. Installnpm
jspm
Includeglobal <link rel="stylesheet" href="libs/mobile-drag-drop/release/default.css">
<script src="libs/mobile-drag-drop/release/index.min.js"></script>
<!--optional import of scroll behaviour-->
<script src="libs/mobile-drag-drop/release/scroll-behaviour.min.js"></script>
<script>
// options are optional ;)
MobileDragDrop.polyfill({
// use this to make use of the scroll behaviour
dragImageTranslateOverride: MobileDragDrop.scrollBehaviourDragImageTranslateOverride
});
</script> SystemJS/JSPM System.import("mobile-drag-drop");
// import css if using system-js css loader plugin
System.import("mobile-drag-drop/default.css!"); ES2015/TypeScript/webpack import {polyfill} from "mobile-drag-drop";
// optional import of scroll behaviour
import {scrollBehaviourDragImageTranslateOverride} from "mobile-drag-drop/scroll-behaviour";
// options are optional ;)
polyfill({
// use this to make use of the scroll behaviour
dragImageTranslateOverride: scrollBehaviourDragImageTranslateOverride
}); Make sure to implement a
If you're targeting iOS Safari 10.x and higher // iOS>=10 supports passive event listeners
// but make sure to catch or check passive event listener support
// regarding this code running on other platforms.
window.addEventListener( 'touchmove', function() {}, {passive: false}); webpack/scss @import "~mobile-drag-drop/default.css"; API & Optionsexport interface Point {
x: number;
y: number;
}
// function signature for the dragImageTranslateOverride hook
export type DragImageTranslateOverrideFn = (
// corresponding touchmove event
event: TouchEvent,
// the processed touch event viewport coordinates
hoverCoordinates: Point,
// the element under the calculated touch coordinates
hoveredElement: HTMLElement,
// callback for updating the drag image offset
translateDragImageFn: (offsetX: number, offsetY: number) => void;
) => void;
export interface Config {
// flag to force the polyfill being applied and not rely on internal feature detection
forceApply?:boolean;
// useful for when you want the default drag image but still want to apply
// some static offset from touch coordinates to drag image coordinates
// defaults to (0,0)
dragImageOffset?:Point;
// if the dragImage shall be centered on the touch coordinates
// defaults to false
dragImageCenterOnTouch?:boolean;
// the drag and drop operation involves some processing. here you can specify in what interval this processing takes place.
// defaults to 150ms
iterationInterval?:number;
// hook for custom logic that decides if a drag operation should start
dragStartConditionOverride?:( event:TouchEvent ) => boolean;
// hook for custom logic that can manipulate the drag image translate offset
dragImageTranslateOverride?:DragImageTranslateOverrideFn;
// hook for custom logic that can override the default action based on the original touch event when the drag never started
// be sure to call event.preventDefault() if handling the default action in the override to prevent the browser default.
defaultActionOverride?:( event:TouchEvent ) => void;
// Drag action delay on touch devices ("hold to drag" functionality, useful for scrolling draggable items). Defaults to no delay.
holdToDrag?:number;
/**
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* THE FOLLOWING OPTIONS ARE ONLY AVAILABLE IN v2.3.0-rc.0
*
*/
// function invoked for each touchstart event to determine if and which touched element is detected as "draggable"
tryFindDraggableTarget?:( event:TouchEvent ) => HTMLElement | undefined;
// function implementing how a copy of the dragged element is created
// NOTE! this function is for customizing HOW an element is transformed to a drag image element
// if you're looking for setting a custom drag image please use [setDragImage()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/setDragImage)
dragImageSetup?:( element:HTMLElement ) => HTMLElement;
// function for determining element that is currently hovered while dragging
// defaults to `document.elementFromPoint()`
elementFromPoint?:( x:number, y:number ) => Element;
/**
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
}
// invoke for initializing the polyfill => returns true if polyfill is applied
export function polyfill(override?: Config):boolean; Custom eventsWhen setting the option
Those events can be used to visualize the DragImage CustomizationIf you want to set a custom drag image use setDragImage(). Override the classes that are applied by the polyfill for customizing the drag image appearance
and snapback behaviour. Mind the .dnd-poly-drag-image {
opacity: .5 !important;
}
/* applied when the drag effect is none and the operation ends */
.dnd-poly-drag-image.dnd-poly-snapback {
-webkit-transition: -webkit-transform 250ms ease-out !important;
-moz-transition: -moz-transform 250ms ease-out !important;
-o-transition: -o-transform 250ms ease-out !important;
transition: transform 250ms ease-out !important;
}
/* applied always as a base class for drop effect styles */
.dnd-poly-drag-icon {
} CSS classes are applied to the There is <link rel="stylesheet" href="[...]/mobile-drag-drop/icons.css"> Custom drag image setup functionOne can also set a custom Checkout the default implementation as a starting point. Known issues and limitations
Contributions welcome! Browser compatibility
Chrome:
Chrome supports touch devices/events. When run on a desktop touch device like MS Surface it turns on touch events
which also disables native drag-and-drop support. Touch events can also be set by a user in Firefox:
Touch events can be activated by a user in Cross-browser differences in HTML5 drag'n'drop APIThe drag'n'drop API is not implemented consistently in all browsers. This table is an effort to list all things required to make drag'n'drop work in all browsers and with the polyfill.
empty cells mean there is nothing special to take into account Polyfill requires dragenter listenerOn desktop browsers if no If a handler is set up it has to call This is pretty bad for the polyfill since JS doesn't allow to check how many listeners were invoked when the event is dispatched,
which forces the polyfill to rely on a listener being present calling Further notices:
Baseline recommendations for cross-browser/-platform support:
ContributeContributions are welcome. For more details on development setup see CONTRIBUTING.md ThanksTo the amazing contributors who've provided massive extensions and fixes to the original. @rem - who created the original demo used to demo this shim's drop-in nature. License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论