因为目前是处于在angular项目中,所以下面分别一个记录简易的angular.js和在angular项目中使用的版本,仅供大家参考。
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项目中
在 angular 项目中可以将上述功能作为 angular 指令在组件中使用。
首先,创建一个名为 `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 { }到此这篇关于angular.js 实现带手柄自由调整页面大小的功能的文章就介绍到这了,更多相关angular.js调整页面大小内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论