使用純JavaScript 自訂HTML 元素大小調整
通常,Web 開發人員需要為HTML 元素提供可調整大小的介面,而不依賴第三方圖書館。本文探討如何使用純 JavaScript 來實現此功能。
要讓 HTML 元素可調整大小,我們可以附加事件偵聽器來監視滑鼠輸入並相應地調整元素的大小。以下是完成此操作的程式碼:
var p = document.querySelector('p'); // Element to make resizable p.addEventListener('click', function init() { p.removeEventListener('click', init, false); p.className = p.className + ' resizable'; var resizer = document.createElement('div'); resizer.className = 'resizer'; p.appendChild(resizer); resizer.addEventListener('mousedown', initDrag, false); }, false); var startX, startY, startWidth, startHeight; function initDrag(e) { startX = e.clientX; startY = e.clientY; startWidth = parseInt(window.getComputedStyle(p).width); startHeight = parseInt(window.getComputedStyle(p).height); document.addEventListener('mousemove', doDrag, false); document.addEventListener('mouseup', stopDrag, false); } function doDrag(e) { p.style.width = startWidth + e.clientX - startX + 'px'; p.style.height = startHeight + e.clientY - startY + 'px'; } function stopDrag(e) { document.removeEventListener('mousemove', doDrag, false); document.removeEventListener('mouseup', stopDrag, false); }
用法:
只需將此程式碼套用至所需的 HTML元素,例如段落(
):
<p>Resizable Text</p>
示範:
[示範連結]
注意:
請記住,此解決方案可能存在跨瀏覽器的兼容性問題;它已經過測試並且可以在 Firefox 中運行,但不能在版本 9 以下的 IE 中運行。
以上是如何僅使用 JavaScript 來調整 HTML 元素的大小?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

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

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3漢化版
中文版,非常好用