В следующем расширенном редакторе управления (CLEditor, available here) захват захвата в нижней части окна позволяет мне перетаскивать вниз и отпускать в порядке, перетаскивать вниз и вверх и отпускать в порядке, но перетаскивание вверх заставляет его никогда не запускать событие endDrag и мне пришлось реализовать тайм-аут kludge. Знаете ли вы, что я могу делать неправильно в jQuery/Javascript?Javascript/jQuery - Почему это событие endDrag не срабатывает должным образом?
Примечание: Я создал пример на jsFiddle here.
<script type="text/javascript">
var textarea, staticOffset;
var iLastMousePos = 0;
var iMin = 160;
var prevTimeout = null;
function startDrag(e) {
console.log('startDrag() event fired');
textarea = $(e.data.el);
textarea.blur();
iLastMousePos = mousePosition(e).y;
staticOffset = textarea.height() - iLastMousePos;
textarea.css('opacity', 0.25);
$(document).mousemove(performDrag).mouseup(endDrag);
return false;
}
function performDrag(e) {
console.log('performDrag() event fired');
var iThisMousePos = mousePosition(e).y;
var iMousePos = staticOffset + iThisMousePos;
// kludge start
// Try implementing without this and the endDrag event won't fire
// if you immediately start dragging upwards
if (iLastMousePos >= (iThisMousePos)) {
console.log('kludge implemented');
iMousePos -= 60;
if (iMousePos < iMin) {
endDrag();
return false;
}
if (!prevTimeout) {
prevTimeout = setTimeout('endDrag();clearTimeout();',600);
}
}
// end kludge
iLastMousePos = iThisMousePos;
iMousePos = Math.max(iMin, iMousePos);
textarea.height(iMousePos + 'px');
if (iMousePos < iMin) {
endDrag();
}
return false;
}
function endDrag() {
console.log('endDrag() event fired');
prevTimeout = null;
$(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
textarea = $('.cleditorMain:first'); // got a better selector?
textarea.css('opacity', 1);
textarea.focus();
textarea = null;
staticOffset = null;
iLastMousePos = 0;
var editor = $("#fldMessage").cleditor()[0];
editor.refresh();
if (!$.browser.msie) { // there's a quirk in IE
editor.focus();
}
}
function mousePosition(e) {
return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop };
};
$(document).ready(function(){
$('#fldMessage').cleditor({
width:'100%',
height:'100%',
useCSS:true,
styles:[["Paragraph", "<p>"], ["Header 1", "<h1>"], ["Header 2", "<h2>"],
["Header 3", "<h3>"], ["Header 4","<h4>"], ["Header 5","<h5>"],
["Header 6","<h6>"], ["Code","<pre>"]],
docCSSFile:"js/jquery.cleditor/jquery.cleditor.doc.css"
}).focus();
// BTW, if you have a more efficient selector than .cleditorMain:first, please let me know
$('.cleditorMain:first').after('<div class="gripper" />');
$('.cleditorMain:first').next('.gripper').css({
'background':'transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABsAAAAFCAMAAACD1meMAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAGUExURbu7u////3iwjPUAAAACdFJOU/8A5bcwSgAAABRJREFUeNpiYMADGHEDBhroAwgwAA9QADeT0qnSAAAAAElFTkSuQmCC) no-repeat scroll center 2px',
'cursor':'s-resize',
'height':'9px',
'overflow':'hidden'
}).bind("mousedown",{el: $('.cleditorMain:first')} , startDrag);
});
</script>
<fieldset style="min-height:160px">
<textarea id="fldMessage" name="fldMessage" rows="4"></textarea>
</fieldset>
У вас есть пример этого где-нибудь? Кроме того, executeDrag (e) окончательный оператор if никогда не может произойти, поскольку вы уверены, что iMousePos никогда не может быть НИЖЕ iMin. – Suroot
@Suroot Я только что создал демонстрацию на jsFiddle. См. Ссылку в исходном вопросе. То, что вы увидите, это то, что вы можете щелкнуть мышкой вниз, хорошо, вы можете щелкнуть вниз, а затем все в порядке, но если вы нажмете и сразу перетащите, это не позволит вам. Комментируйте блок «kludge», и он все равно потерпит неудачу. – Volomike