简体   繁体   English

如何在 VUE 中更改 CSS

[英]How to change CSS in VUE

I have an HTML element:我有一个 HTML 元素:

<div id="scrollbar" style="background-color: #000;" 
     v-bind:style="{ height : scrollbarheight + 'px' }"
></div>

I'm trying to change height like this:我正在尝试像这样改变高度:

let scrollbarheight = 30

function jsonMSG(msg) {
  const cmd = JSON.parse(msg)
  console.log('CMD: ', cmd.height)
  const h = Math.round(cmd.height / 1024)
  scrollbarheight = h
}

function webSocketOnMSG(msg) {
  if (typeof (msg.data) === 'string') {
    jsonMSG(msg.data)
    return
  }
}

data() {
    return {
      scrollbarheight
      ...
    }
}
created() {
    console.log('Starting Connection to WebSocket')
    this.connection = new WebSocket('ws://127.0.0.1:8080/')
    // this.connection = new WebSocket('ws://echo.websocket.org')
    this.connection.onopen = function (event) {
      console.log(event)
      console.log('Success')
    }
    this.connection.onmessage = webSocketOnMSG
}

But it doesn't work.但它不起作用。

Also, how can I smoothly drag div but only vertically (like scrollbar)?另外,我怎样才能平滑地拖动 div 但只能垂直拖动(如滚动条)?

I think your problem is that you put your functions outside the methods .我认为您的问题是您将函数放在methods之外。 Bring them to methods and use this to call them and change the this.scrollbarheight variable.将它们带到methods中并使用this来调用它们并更改this.scrollbarheight变量。

I provide a code snippet to simulate your situation.我提供了一个代码片段来模拟您的情况。

 var app = new Vue({ el: '#app', data() { return { scrollbarheight: 30 } }, methods: { increaseHeight() { this.scrollbarheight += 30 } } })
 <script src="https://unpkg.com/vue/dist/vue.min.js"></script> <div id="app"> <div id="scrollbar" style="background-color: #000;" v-bind:style="{ height: scrollbarheight + 'px' }"></div> <button @click="increaseHeight()"> Increase </button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM