html - 如何在移动设备上调整 div 大小(单击角落)?

标签 html mobile resize

我需要一个 div,您可以在 Android 和 iPhone 上单击角落并调整大小

感谢您的帮助

最佳答案

这是我的解决方案。此代码适用于 Firefox、Chrome、iPad 和 Android。只需点击角落即可。

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>resizable demo</title>
<link rel="stylesheet"
    href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<style>
#resizable {
    width: 100px;
    height: 100px;
    background: #ccc;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>

    <script type="text/javascript">
    (function ($) {
        // Detect touch support
        $.support.touch = 'ontouchend' in document;
        // Ignore browsers without touch support
        if (!$.support.touch) {
        return;
        }
        var mouseProto = $.ui.mouse.prototype,
            _mouseInit = mouseProto._mouseInit,
            touchHandled;

        function simulateMouseEvent (event, simulatedType) { //use this function to simulate mouse event
        // Ignore multi-touch events
            if (event.originalEvent.touches.length > 1) {
            return;
            }
        event.preventDefault(); //use this to prevent scrolling during ui use

        var touch = event.originalEvent.changedTouches[0],
            simulatedEvent = document.createEvent('MouseEvents');
        // Initialize the simulated mouse event using the touch event's coordinates
        simulatedEvent.initMouseEvent(
            simulatedType,    // type
            true,             // bubbles                    
            true,             // cancelable                 
            window,           // view                       
            1,                // detail                     
            touch.screenX,    // screenX                    
            touch.screenY,    // screenY                    
            touch.clientX,    // clientX                    
            touch.clientY,    // clientY                    
            false,            // ctrlKey                    
            false,            // altKey                     
            false,            // shiftKey                   
            false,            // metaKey                    
            0,                // button                     
            null              // relatedTarget              
            );

        // Dispatch the simulated event to the target element
        event.target.dispatchEvent(simulatedEvent);
        }
        mouseProto._touchStart = function (event) {
        var self = this;
        // Ignore the event if another widget is already being handled
        if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
            return;
            }
        // Set the flag to prevent other widgets from inheriting the touch event
        touchHandled = true;
        // Track movement to determine if interaction was a click
        self._touchMoved = false;
        // Simulate the mouseover event
        simulateMouseEvent(event, 'mouseover');
        // Simulate the mousemove event
        simulateMouseEvent(event, 'mousemove');
        // Simulate the mousedown event
        simulateMouseEvent(event, 'mousedown');
        };

        mouseProto._touchMove = function (event) {
        // Ignore event if not handled
        if (!touchHandled) {
            return;
            }
        // Interaction was not a click
        this._touchMoved = true;
        // Simulate the mousemove event
        simulateMouseEvent(event, 'mousemove');
        };
        mouseProto._touchEnd = function (event) {
        // Ignore event if not handled
        if (!touchHandled) {
            return;
        }
        // Simulate the mouseup event
        simulateMouseEvent(event, 'mouseup');
        // Simulate the mouseout event
        simulateMouseEvent(event, 'mouseout');
        // If the touch interaction did not move, it should trigger a click
        if (!this._touchMoved) {
          // Simulate the click event
          simulateMouseEvent(event, 'click');
        }
        // Unset the flag to allow other widgets to inherit the touch event
        touchHandled = false;

        };
        mouseProto._mouseInit = function () {
        var self = this;
        // Delegate the touch handlers to the widget's element
        self.element
            .on('touchstart', $.proxy(self, '_touchStart'))
            .on('touchmove', $.proxy(self, '_touchMove'))
            .on('touchend', $.proxy(self, '_touchEnd'));

        // Call the original $.ui.mouse init method
        _mouseInit.call(self);
        };
    })(jQuery);


    </script>

    <div id="resizable"></div>

    <script>
$( "#resizable" ).resizable();
</script>

</body>
</html>

关于html - 如何在移动设备上调整 div 大小(单击角落)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17682641/

相关文章:

javascript - ResizeObserver 在 Windows 上无法按预期工作

html - 调整大小时 float div

jquery - 使用 jQuery 使一个 div 填充整个表格单元格

javascript - 检查 "this"的元素类型

html - 为什么这2个div之间有差距

iphone - 针对多个移动平台的最具成本效益的方式

flutter 错误 - 'package:flutter/src/painting/decoration_image.dart' : Failed assertion: line 50 pos 15: 'image != null' : is not true

c# - 当无法访问 Web 服务 URL 时,如何在 Windows Mobile 中显示 "cannot connect"对话框?

html - 使用CSS 3在div内输入时扩展搜索输入字段

jquery - 在窗口调整大小时获取 div 的实时宽度?