Angular.js?實(shí)現(xiàn)帶手柄自由調(diào)整頁(yè)面大小的功能
因?yàn)槟壳笆翘幱谠赼ngular項(xiàng)目中,所以下面分別一個(gè)記錄簡(jiǎn)易的angular.js和在angular項(xiàng)目中使用的版本,僅供大家參考。
Angular.js
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Resizable Element with Angular Directive</title> <style> .resizable { width: 200px; height: 200px; background-color: lightgray; border: 1px solid #ccc; overflow: auto; position: relative; } .resize-handle { width: 10px; height: 10px; background-color: #000; position: absolute; bottom: 0; right: 0; cursor: nwse-resize; } </style> </head> <body> <div ng-app="resizableApp"> <div ng-controller="ResizableController"> <div resizable></div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script> <script> angular.module('resizableApp', []) .controller('ResizableController', function($scope) { // 可以在這里添加控制器邏輯 }) .directive('resizable', function() { return { restrict: 'A', link: function(scope, element) { const resizableElement = element[0]; const resizeHandle = document.createElement('div'); resizeHandle.classList.add('resize-handle'); resizableElement.appendChild(resizeHandle); let isResizing = false; let initialX; let initialY; let originalWidth; let originalHeight; resizeHandle.addEventListener('mousedown', function(event) { event.preventDefault(); isResizing = true; initialX = event.clientX; initialY = event.clientY; originalWidth = parseFloat(getComputedStyle(resizableElement, null).getPropertyValue('width')); originalHeight = parseFloat(getComputedStyle(resizableElement, null).getPropertyValue('height')); document.addEventListener('mousemove', resize); document.addEventListener('mouseup', stopResize); }); function resize(event) { if (isResizing) { const width = originalWidth + (event.clientX - initialX); const height = originalHeight + (event.clientY - initialY); resizableElement.style.width = width + 'px'; resizableElement.style.height = height + 'px'; } } function stopResize() { isResizing = false; document.removeEventListener('mousemove', resize); document.removeEventListener('mouseup', stopResize); } } }; }); </script> </body> </html>
在Angular項(xiàng)目中
在 Angular 項(xiàng)目中可以將上述功能作為 Angular 指令在組件中使用。
首先,創(chuàng)建一個(gè)名為 `resizable` 的自定義指令
import { Directive, ElementRef, HostListener } from '@angular/core'; @Directive({ selector: '[resizable]' }) export class ResizableDirective { private isResizing = false; private initialX: number; private initialY: number; private originalWidth: number; private originalHeight: number; constructor(private elementRef: ElementRef) {} @HostListener('document:mousemove', ['$event']) onMouseMove(event: MouseEvent) { if (this.isResizing) { const width = this.originalWidth + (event.clientX - this.initialX); const height = this.originalHeight + (event.clientY - this.initialY); this.elementRef.nativeElement.style.width = width + 'px'; this.elementRef.nativeElement.style.height = height + 'px'; } } @HostListener('document:mouseup') onMouseUp() { this.isResizing = false; } @HostListener('mousedown', ['$event']) onMouseDown(event: MouseEvent) { event.preventDefault(); this.isResizing = true; this.initialX = event.clientX; this.initialY = event.clientY; this.originalWidth = parseFloat(getComputedStyle(this.elementRef.nativeElement).getPropertyValue('width')); this.originalHeight = parseFloat(getComputedStyle(this.elementRef.nativeElement).getPropertyValue('height')); } }
在組件模板中使用該指令
<div resizable class="resizable"></div>
最后,將 `ResizableDirective` 添加到您的 Angular 模塊中
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { ResizableDirective } from './resizable.directive'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent, ResizableDirective ], imports: [ BrowserModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
到此這篇關(guān)于Angular.js 實(shí)現(xiàn)帶手柄自由調(diào)整頁(yè)面大小的功能的文章就介紹到這了,更多相關(guān)Angular.js調(diào)整頁(yè)面大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
AngularJS對(duì)動(dòng)態(tài)增加的DOM實(shí)現(xiàn)ng-keyup事件示例
這篇文章主要介紹了AngularJS對(duì)動(dòng)態(tài)增加的DOM實(shí)現(xiàn)ng-keyup事件示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03AngularJS入門(mén)教程之XHR和依賴(lài)注入詳解
本文主要介紹AngularJS XHR和依賴(lài)注入,這里整理了詳細(xì)資料和示例代碼,有興趣的小伙伴可以參考下2016-08-08AngularJS實(shí)踐之使用NgModelController進(jìn)行數(shù)據(jù)綁定
大家都知道AngularJS中的指令是其尤為復(fù)雜的一個(gè)部分,但是這也是其比較好玩的地方。這篇文章我們就來(lái)說(shuō)一說(shuō)如何在我們自定義的指令中,利用ngModel的controller來(lái)做雙向數(shù)據(jù)綁定,本文對(duì)大家學(xué)習(xí)AngularJS具有一定的參考借鑒價(jià)值,有需要的朋友們可以參考借鑒。2016-10-10Angular.js 實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字實(shí)例代碼
這篇文章主要介紹了Angular.js 實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問(wèn)題
今天小編就為大家分享一篇快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08