最近學習了Vue.js
找到了計算屬性computed,於是寫了個栗子玩玩,於是就有了下面的這些
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!-- 開發環境版本,包含了有幫助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 生產環境版本,優化了尺寸和速度 -->
<!-- <script src="https://cdn.jsdelivr.net/npm/vue"></script> -->
<title>可以拉長的div</title>
</head>
<body>
<div id="app">
<div :style="wh" style="background-color: green;">
{{wh}}
</div>
</div>
</body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
wh:{
width:250,
height:250
}
},
computed:{
wh:{
width:function(){
return this.wh.width + "px";
},
height:function(){
return this.wh.height + "px";
}
}
},
methods:{
},
watch: {
}
});
</script>
</html>
執行的結果卻是 { "width": 250, "height": 250 }
,???怎麼沒有生效,寫錯了??,去網上搜了一下,都是跟官網一樣寫的栗子,我就…
仔細分析了一下,原來裏面寫的是get和set方法,這樣寫不會識別,而且名字與data中的重複了,vue優先使用了data中的
更改了一下,
computed:{
wh2:function(){
return {
width:this.wh.width + "px",
height:this.wh.height + "px"
}
}
},
<div :style="wh2" style="background-color: green;">
以下是我寫的Demo的半成品
<!DOCTYPE html>
<html>
<head>
<base href="../../">
<meta charset="utf-8" />
<!-- 開發環境版本,包含了有幫助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 生產環境版本,優化了尺寸和速度 -->
<!-- <script src="https://cdn.jsdelivr.net/npm/vue"></script> -->
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<title>vue示例</title>
<style type="text/css">
#app{
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="app" @mouseup="mouseUp" @mousemove="mouseMove">
<div @mousedown="mouseDown" :style="[styleObj,styleObj2]" style="background-color: green;">
</div>
</div>
</body>
<script type="text/javascript">
var app = new Vue({
el:"#app",
data:{
mouseIsClick:false,
mouseLayerX:0,
mouseLayerY:0,
styleObj:{
width:250,
height:250,
cursor:"auto"
},
},
computed:{
styleObj2:function(){
return {
width:this.styleObj.width + "px",
height:this.styleObj.height + "px"
}
}
},
methods:{
//鼠標按下時
mouseDown:function(e){
//按下的狀態
this.mouseIsClick = true;
this.mouseLayerX = e.layerX;
this.mouseLayerY = e.layerY;
},
//鼠標鬆開時
mouseUp:function(e){
//清除按下的狀態
if(this.mouseIsClick){
this.mouseIsClick = false;
}
},
//鼠標移動時
mouseMove:function(e){
// e = e || window.event; // 事件
// let target = e.target || e.srcElement; // 獲得事件源
let maxWidth = this.styleObj.width;
let minWidth = this.styleObj.width - 5;
let maxHeight = this.styleObj.height;
let minHeight = this.styleObj.height - 5;
if((minWidth <= e.layerX && e.layerX <= maxWidth) && (minHeight <= e.layerY && e.layerY <= maxHeight)){
this.styleObj.cursor = "move";
//如果出現了小手,則證明在我們規定的可拖動區
}else if(minWidth <= e.layerX && e.layerX <= maxWidth){
this.styleObj.cursor = "e-resize";
}else if(minHeight <= e.layerY && e.layerY <= maxHeight){
this.styleObj.cursor = "n-resize";
}else{
this.styleObj.cursor = "auto";
}
//如果鼠標按下了,則根據鼠標的移動更改寬和高
if(this.mouseIsClick){
// if((minWidth - 10) <= e.layerX || (minHeight - 10) <= e.layerY){
let x = e.layerX - this.mouseLayerX;
let y = e.layerY - this.mouseLayerY;
this.styleObj.width += x;
this.styleObj.height += y;
this.mouseLayerX = e.layerX;
this.mouseLayerY = e.layerY;
// }
}
},
out:function(e){
}
},
watch: {
}
});
</script>
</html>
可以拖動這個綠方塊,會變大,往裏拖會變小,在右邊和下邊會顯示出左右箭頭和上線箭頭,和移動箭頭(emm是不是叫指針能好點),但是現在有寫不滿意,以後有時間再改改
這是我的解決方案,歡迎補充