up1/static/js/dragresize.js

100 lines
2.5 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-07 20:45:06 +02:00
var MIN_WIDTH_PX = 100
var MAX_WIDTH_RATIO = 100
2015-06-07 01:27:45 +02:00
$(document).on('mousedown', '.dragresize', function (e) {
2015-06-07 01:19:06 +02:00
if (e.which && 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) {
2015-06-07 20:45:06 +02:00
minw = MIN_WIDTH_PX
minh = MIN_WIDTH_PX * (dragsizeh / dragsizew)
maxw = dragsizew * MAX_WIDTH_RATIO
maxh = (dragsizew * MAX_WIDTH_RATIO) * (dragsizeh / dragsizew)
2015-06-07 01:00:50 +02:00
} else {
2015-06-07 20:45:06 +02:00
minh = MIN_WIDTH_PX
minw = MIN_WIDTH_PX * (dragsizew / dragsizeh)
maxh = dragsizeh * MAX_WIDTH_RATIO
maxw = (dragsizeh * MAX_WIDTH_RATIO) * (dragsizew / dragsizeh)
2015-06-07 01:00:50 +02:00
}
2015-06-24 03:40:46 +02:00
lastx = e.clientX
lasty = e.clientY
2015-06-06 23:44:42 +02:00
})
2015-06-07 01:27:45 +02:00
$(document).on('mousemove', function (e) {
2015-06-06 23:44:42 +02:00
if (!dragging) {
return
}
2015-06-10 17:45:02 +02:00
2015-06-24 03:40:46 +02:00
var px = e.clientX
var py = e.clientY
var newx = px - lastx
var newy = py - lasty
2015-06-06 23:44:42 +02:00
if (Math.abs(newx) < 1 && Math.abs(newy) < 1) {
return;
}
e.preventDefault();
2015-06-10 17:45:02 +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:09:01 +02:00
dragging.css({ 'width': Math.min(maxw, Math.max(width + (width * (.0025 * newx)), minw)) + 'px', 'height': 'auto' })
2015-06-06 23:44:42 +02:00
} else {
2015-06-07 01:09:01 +02:00
dragging.css({ 'height': Math.min(maxh, Math.max(height + (height * (.0025 * newy)), minh)) + 'px', 'width': 'auto' })
2015-06-06 23:44:42 +02:00
}
lastx = px
lasty = py
2015-06-06 23:44:42 +02:00
})
2015-06-07 01:27:45 +02:00
$(document).on('mouseup', function (e) {
2015-06-06 23:44:42 +02:00
if (!dragging) {
return
}
dragging.removeClass('dragging')
dragging = undefined
});
})