使用CSS Transform Scale 進行jQuery 拖曳/調整大小
當對元素應用帶有縮放功能的CSS 變換,然後使用jQuery的可拖曳時,會出現此問題() 和resizing() 插件在該元素內的子元素上。問題是,由於應用的比例,基於 jQuery 的變更看起來與滑鼠移動「不同步」。
原始解決方案
找到了修補的解決方案StackOverflow 上建議修改 jQuery 插件。具體來說,對_mouseStart() 和_generatePosition() 函數進行了更改以考慮縮放:
- Monkey-patched _mouseStart() 以等於所應用的縮放比例的因子調整偏移值。
- 在 _generatePosition() 中,根據比例因子調整移動,以確保拖曳/調整大小的元素正確對齊。
改進的解決方案
隨後,發現了更優雅的解決方案,無需修改 jQuery。此方法涉及為可調整大小和可拖曳事件設定回調處理程序:
可調整大小:
$(this).resizable({ // Set very large and negative minWidth and minHeight minWidth: -(contentElem.width()) * 10, minHeight: -(contentElem.height()) * 10, resize: function(event, ui) { // Calculate the change in width and height var changeWidth = ui.size.width - ui.originalSize.width; var changeHeight = ui.size.height - ui.originalSize.height; // Adjust the new width and height by dividing the change by the zoomScale var newWidth = ui.originalSize.width + changeWidth / zoomScale; var newHeight = ui.originalSize.height + changeHeight / zoomScale; // Update the ui.size object with the adjusted values ui.size.width = newWidth; ui.size.height = newHeight; } });
可拖曳:
$(this).draggable({ handle: '.drag-handle', // Specify a handle element for dragging start: function(event, ui) { // Reset the ui.position object to {left: 0, top: 0} at the start of the drag ui.position.left = 0; ui.position.top = 0; }, drag: function(event, ui) { // Calculate the change in left and top positions var changeLeft = ui.position.left - ui.originalPosition.left; var changeTop = ui.position.top - ui.originalPosition.top; // Adjust the new left and top positions by dividing the change by the zoomScale var newLeft = ui.originalPosition.left + changeLeft / zoomScale; var newTop = ui.originalPosition.top + changeTop / zoomScale; // Update the ui.position object with the adjusted values ui.position.left = newLeft; ui.position.top = newTop; } });
透過使用這些回調處理程序,可拖曳和可調整大小的元素現在將在縮放元素內正確運行,而無需修改jQuery 或實現複雜的修補解決方案。
以上是如何使用 jQuery 的 Draggable() 和 resizing() 外掛以及子元素上的 CSS 變換比例?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1
強大的PHP整合開發環境