up1/static/js/dragresize.js

90 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-06-06 23:44:42 +02:00
$(function () {
var dragging
var lastx
var lasty
2015-06-07 01:00:50 +02:00
var dragsizew
var dragsizeh
2015-06-07 01:06:52 +02:00
var minw
var minh
var maxw
var maxh
2015-06-07 00:20:47 +02:00
$(document).on('dblclick', '.dragresize', function (e) {
var target = $(e.target)
target.toggleClass('full')
if (target.hasClass('full')) {
target.addClass('dragged')
target.width(e.target.naturalWidth)
target.height(e.target.naturalHeight)
} else {
target.removeClass('dragged')
target.width('auto')
target.height('auto')
}
})
2015-06-06 23:44:42 +02:00
$(document).on('mousedown', '.dragresize', function(e) {
2015-06-07 00:24:47 +02:00
if (e.which != 1) {
2015-06-07 00:20:47 +02:00
return
}
2015-06-07 00:28:55 +02:00
e.preventDefault();
2015-06-06 23:44:42 +02:00
dragging = $(e.target)
dragging.addClass('dragging')
2015-06-07 01:00:50 +02:00
dragsizew = e.target.naturalWidth
dragsizeh = e.target.naturalHeight
if (dragsizew > dragsizeh) {
minw = 100
minh = 100 * (dragsizeh / dragsizew)
2015-06-07 01:06:52 +02:00
maxw = dragsizew * 10
maxh = (dragsizew * 10) * (dragsizeh / dragsizew)
2015-06-07 01:00:50 +02:00
} else {
minh = 100
minw = 100 * (dragsizew / dragsizeh)
2015-06-07 01:06:52 +02:00
maxh = dragsizeh * 10
maxw = (dragsizeh * 10) * (dragsizew / dragsizeh)
2015-06-07 01:00:50 +02:00
}
2015-06-06 23:44:42 +02:00
lastx = e.pageX
lasty = e.pageY
})
$(document).on('mousemove', function (e) {
if (!dragging) {
return
}
2015-06-07 00:20:47 +02:00
e.preventDefault();
2015-06-06 23:44:42 +02:00
var newx = e.pageX - lastx
var newy = e.pageY - lasty
2015-06-07 00:20:47 +02:00
2015-06-06 23:44:42 +02:00
var width = dragging.width()
var height = dragging.height()
dragging.addClass('dragged')
if (Math.abs(newx) > Math.abs(newy)) {
2015-06-07 01:06:52 +02:00
dragging.css({ 'width': Math.min(maxw, Math.max(width + (width * (.01 * newx)), minw)) + 'px', 'height': 'auto' })
2015-06-06 23:44:42 +02:00
} else {
2015-06-07 01:06:52 +02:00
dragging.css({ 'height': Math.min(maxh, Math.max(height + (height * (.01 * newy)), minh)) + 'px', 'width': 'auto' })
2015-06-06 23:44:42 +02:00
}
lastx = e.pageX
lasty = e.pageY
})
$(document).on('mouseup', function (e) {
if (!dragging) {
return
}
dragging.removeClass('dragging')
dragging = undefined
});
})