GithubHelp home page GithubHelp logo

Comments (9)

Yohn avatar Yohn commented on July 2, 2024 3

heres what I've been using.., thanks to hompimpaalaihumgambreng

jquery

$('.modal').drags({handle: '.modal-header'})
// Simple JQuery Draggable Plugin
// https://plus.google.com/108949996304093815163/about
// http://hompimpaalaihumgambreng.blogspot.com/2012/06/draggable-without-jquery-ui.html#.UJ4VxmckTg0
// Usage: $(selector).drags();
// Options:
// handle => your dragging handle.
//           If not defined, then the whole body of the
//           selected element will be draggable
// cursor => define your draggable element cursor type

(function($) {
    $.fn.drags = function(opt) {

        opt = $.extend({handle:"",cursor:"move"}, opt);

        if(opt.handle === "") {
            var $el = this;
        } else {
            var $el = this.find(opt.handle);
        }

        return $el.css('cursor', opt.cursor).on("mousedown", function(e) {
            if(opt.handle === "") {
                var $drag = $(this).addClass('draggable');
            } else {
                var $drag = $(this).addClass('active-handle').parent().addClass('draggable');
            }
            var z_idx = $drag.css('z-index'),
                drg_h = $drag.outerHeight(),
                drg_w = $drag.outerWidth(),
                pos_y = $drag.offset().top + drg_h - e.pageY,
                pos_x = $drag.offset().left + drg_w - e.pageX;

//          $drag.css('z-index', 1000).parents().on("mousemove", function(e) {
            $drag.parents().on("mousemove", function(e) {
                $('.draggable').offset({
                    top:e.pageY + pos_y - drg_h,
                    left:e.pageX + pos_x - drg_w
                }).on("mouseup", function() {
                    $(this).removeClass('draggable').css('z-index', z_idx);
                });
            });
            e.preventDefault(); // disable selection
        }).on("mouseup", function() {
            if(opt.handle === "") {
                $(this).removeClass('draggable');
            } else {
                $(this).removeClass('active-handle').parent().removeClass('draggable');
            }
        });

    }
})(jQuery);

from bootstrap-modal.

jschr avatar jschr commented on July 2, 2024

Hey, thanks for the issue but I don't think I will be implementing this directly in Bootstrap Modal at this time. I think it's a bit out of scope for this plugin and as @Yohn suggests, there are a few libraries dedicated to this functionality already.

from bootstrap-modal.

gbelmm avatar gbelmm commented on July 2, 2024

extend option ?

$().modal({ drags: true })

from bootstrap-modal.

jschr avatar jschr commented on July 2, 2024

The issue I have with this is I would need to either:

  1. Include a library into Bootstrap-Modal
  2. Test if a library exists in Bootstrap-Modal

Both involve to some degree choosing a library, I would much rather the developer have that choice. It will also muddy up the code to directly include a library, especially when the feature is really only desirable in a desktop enviroment. This is why I believe it to be out of scope for this plugin. As @Yohn pointed out in his example, it is fairly trivial to add the support yourself.

from bootstrap-modal.

emage avatar emage commented on July 2, 2024

@Yohn

I am using your drag function, however, is it possible to reset to its original position upon relaunch?

Currently, if I close the modal and open it again, it stays at the previously dragged position.. So if i dragged one of my modal off screen a bit, the next one is stayed off screen.

Thanks

from bootstrap-modal.

Yohn avatar Yohn commented on July 2, 2024

@emage

what you could do is when the modal is shown, get the offset so you have the original placement - var offset = $('.modal').offset(); , and then when its hidden reset the offset positions to the original placement.. $('.modal').offset({top: offset.top, left: offset.left})

I havent tested this as I havent played with it in a few months, but in theory it should work.

from bootstrap-modal.

anandu4u avatar anandu4u commented on July 2, 2024

@Yohn

Thanks for the code. It's working for me.
The issue I have with this is, is it possible to reset to its original position upon relaunch?

from bootstrap-modal.

Yohn avatar Yohn commented on July 2, 2024

I believe you could do something like $('.modal').modal('layout') to reposition the modal to the center of the screen.

from bootstrap-modal.

ptma avatar ptma commented on July 2, 2024

@anandu4u

Reset to original position :
$('body').on('hidden.bs.modal', function() { $('.modal').css({'top': '', left:''}); });

from bootstrap-modal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.