vue.js - 绑定类更改但 div 不会移动
问题描述
我有一个带有 3 个可移动面板的侧边栏。我通过使用(使用 TailwindCSS 类)更改绑定:class
到父级来移动它。当我单击菜单按钮时,我看到 DOM 确实会做出反应并使用正确的值更改类,但面板不会移动。<div>
-translate-x-(amount)
translate-x-
链接到沙箱(由于某种原因它可以在沙箱上运行 - 它也可以在我的 PC 上随机运行,但很少): https ://codesandbox.io/s/hopeful-cdn-o38n6?file=/src/App.vue
这是代码:
<template>
<div class="h-screen flex">
<div class="flex container w-40 h-80 bg-black mx-auto my-auto text-black overflow-hidden">
<div class="sidebar container flex transform duration-1000" :class="position">
<div class="sidebar-menu flex-shrink-0 w-40 transform duration-1000 bg-gray-300">
<button @click="currentIndex++">Menu item</button>
</div>
<div class="sidebar-menu flex-shrink-0 w-40 bg-blue-300 transform duration-1000">
<div class="flex flex-col">
<button class="flex justify-start" @click="currentIndex--">
Go back
</button>
<button class="flex justify-start" @click="currentIndex++">
Menu item
</button>
</div>
</div>
<div class="sidebar-menu flex-shrink-0 w-40 transform duration-1000 bg-red-300">
<div class="flex flex-col">
<button class="flex justify-start" @click="currentIndex--">
Go back
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data: () => ({
sidebarWidth: 40,
currentIndex: 0
}),
computed: {
position() {
if (this.currentIndex === 0) {
return 'translate-x-0'
} else {
return `-translate-x-${this.sidebarWidth * this.currentIndex}`}
}
},
}
</script>
解决方案
为了跟踪您的错误原因,我看到您的代码有一个演示版本,例如codesanbox。
演示
const app = new Vue({
el: '#root',
data: {
sidebarWidth: 40,
currentIndex: 0,
message: 'TailWindCSS Dev',
model: null,
},
computed: {
position() {
if (this.currentIndex === 0) {
return 'translate-x-0'
} else {
return `-translate-x-${this.sidebarWidth * this.currentIndex}`}
}
},
});
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
<div id="root">
<div class="flex justify-center">
<p class="text-3xl text-yellow-600">{{message}}</p>
</div>
<template>
<div class="h-screen flex">
<div class="flex container w-40 h-80 bg-black mx-auto my-auto text-black overflow-hidden">
<div class="sidebar container flex transform duration-1000" :class="position">
<div class="sidebar-menu flex-shrink-0 w-40 transform duration-1000 bg-gray-300">
<button @click="currentIndex++">Menu item</button>
</div>
<div class="sidebar-menu flex-shrink-0 w-40 bg-blue-300 transform duration-1000">
<div class="flex flex-col">
<button class="flex justify-start" @click="currentIndex--">
Go back
</button>
<button class="flex justify-start" @click="currentIndex++">
Menu item
</button>
</div>
</div>
<div class="sidebar-menu flex-shrink-0 w-40 transform duration-1000 bg-red-300">
<div class="flex flex-col">
<button class="flex justify-start" @click="currentIndex--">
Go back
</button>
</div>
</div>
</div>
</div>
</div>
</template>
</div>
推荐阅读
- avfoundation - AVFoundation - 当我在视频上叠加贴纸时,视频文件大小会增加
- r - R数据帧的行组的条件语句
- json - 如何从 KStream 中提取 Json 值
- flutter - 如何在颤振应用程序中将分隔线扩展到整个窗口宽度?
- javascript - 循环遍历动态表时遇到问题
- python - QLineEdit 更改 PlaceholderText 颜色
- microsoft-graph-api - 如何在 msgraph-sdk-dotnet-auth 身份验证提供程序中设置 MaxRetry 值
- gsm - 配置 NetworkManager 以在蜂窝连接处于活动状态时通过 WiFi 路由本地 wlan 数据以进行调试
- gnucobol - GnuCOBOL 微不足道的“回声”功能
- python - 更改熊猫数据框中的数据类型更改过滤结果