This article mainly introduces the detailed method of adding drag and drop to Bootstrap Modal. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
There are many drag-and-drop implementations for Bootstrap Modal on the Internet, but many of the codes look relatively complicated and may not be very encapsulated. Some even use the drag-and-drop function of jquery ui. These are not mine. What I want, in fact, I was adding drag and drop functionality to Bootstrap Modal during the Spring Festival of 2017, and a year passed in a blink of an eye. During the Spring Festival of 2017, some colleagues in the previous project used layer as the front end, but for back-end development like me, who has a little knowledge of js front-end, it was difficult for me to introduce a whole set of things like layer and layui. It used to be roughly After browsing some components of layui, I found that the functions of the components are not very complete. It is foreseeable that slightly more complex projects will cause a lot of pitfalls in the later stage. Therefore, bootstrap was used as the front-end CSS framework, and components such as modal were also used extensively to make pop-up windows.
Steps (based on 3.3.4 or higher 3.x version):
1. Open the bootstrap.js source code to find the modal component code block, in Modal Add drag and drop code implementation under the .DEFAULTS code block.
Modal.DEFAULTS = { backdrop: true, keyboard: true, show: true } //新加入的拖拽 Modal.prototype.draggable = function () { var $ele = this.$element; var mouseOffset; var $modalDialog = $ele.find(".modal-dialog"); var dialogOffset; $ele.find(".modal-header").on('mousedown', function (event) { $(this).addClass({cursor: 'move'}); $('body').addClass('select'); dialogOffset = $modalDialog.offset(); mouseOffset = { top: event.pageY - dialogOffset.top, left: event.pageX - dialogOffset.left }; $('body').on("mousemove", function (event) { var left = event.pageX - mouseOffset.left; var top = event.pageY - mouseOffset.top; if (left < 10) { left = 0; } else if (left > $(window).width() - $modalDialog.width()) { left = $(window).width() - $modalDialog.width(); } if (top < 10) { top = 0; } else if (top > $(window).height() - $modalDialog.height()) { top = $(window).height() - $modalDialog.height(); } $modalDialog.offset({ top: top, left: left }); }); }); $(document).on("mouseup mouseleave", function () { $('body').off("mousemove"); }); }
2. Add and call the draggable method in the show method of modal
##
Modal.prototype.show = function (_relatedTarget) { var that = this var e = $.Event('show.bs.modal', {relatedTarget: _relatedTarget}) this.$element.trigger(e) if (this.isShown || e.isDefaultPrevented()) return this.isShown = true //调用draggable()增加拖拽 this.draggable() this.checkScrollbar() this.setScrollbar() this.$body.addClass('modal-open') this.escape() this.resize() //......省略 }After completing the above operation, directly Introducing the newly modified source code js can already support drag and drop well on the page, and the entire drag and drop is within the scope of the visual window and will not exceed the boundary.
Note: After completing the above source code modification, it is best to compress and package the bootstrap source code. Those who are not familiar with automatic construction can put it on an open website for compression. Those who are familiar with automatic construction are best to use it. Front-end automation tools such as gulp and webpack are used to package and compress source code to reduce bandwidth usage during web page loading. Of course, it is also recommended to name the file something like bootstrap-draggable.min.js so that you can clearly guess how this file is different from the original file. In addition, you can also directly download the source code of the bootstrap3.x version, then modify the modal.js inside, and then use its own grunt build to repackage it.
The problem arises: The source code of the bootstrap 4.0+ version does not have much modifications to the modal component compared to the 3.x version. The above method can also be adopted. Including centering the modal, you can also modify the source code to complete it.
JS implementation of simple drag-and-drop shopping cart function
TP5 integrates webuploader to implement preview drag-and-drop search Delete
Example to explain how jQuery uses the zTree plug-in to implement drag-and-drop functionality
The above is the detailed content of Bootstrap Modal adds drag-and-drop method example. For more information, please follow other related articles on the PHP Chinese website!

一个好的网站,不能只看外表,网站后台同样很重要。本篇文章给大家分享10款好看又实用的Bootstrap后台管理系统模板,可以帮助大家快速建立强大有美观的网站后台,欢迎下载使用!如果想要获取更多后端模板,请关注php中文网后端模板栏目!

bootstrap与jquery的关系是:bootstrap是基于jquery结合了其他技术的前端框架。bootstrap用于快速开发Web应用程序和网站,jquery是一个兼容多浏览器的javascript库,bootstrap是基于HTML、CSS、JAVASCRIPT的。

好看又实用的Bootstrap电商源码模板可以提高建站效率,下面本文给大家分享7款实用响应式Bootstrap电商源码,均可免费下载,欢迎大家使用!更多电商源码模板,请关注php中文网电商源码栏目!

好看又实用的企业公司网站模板可以提高您的建站效率,下面PHP中文网为大家分享8款Bootstrap企业公司网站模板,均可免费下载,欢迎大家使用!更多企业站源码模板,请关注php中文网企业站源码栏目!

在bootstrap中,sm是“小”的意思,是small的缩写;sm常用于表示栅格类“.col-sm-*”,是小屏幕设备类的意思,表示显示大小大于等于768px并且小于992px的屏幕设备,类似平板设备。

bootstrap modal关闭的方法:1、连接好bootstrap的插件;2、给按钮绑定模态框事件;3、通过“ $('#myModal').modal('hide');”方法手动关闭模态框即可。

bootstrap默认字体大小是“14px”;Bootstrap是一个基于HTML、CSS、JavaScript的开源框架,用于快速构建基于PC端和移动端设备的响应式web页面,并且默认的行高为“20px”,p元素行高为“10px”。

bootstrap是免费的;bootstrap是美国Twitter公司的设计师“Mark Otto”和“Jacob Thornton”合作基于HTML、CSS、JavaScript 开发的简洁、直观、强悍的前端开发框架,开发完成后在2011年8月就在GitHub上发布了,并且开源免费。


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

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

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
