【问题标题】:Toggle between two colors vueJS在两种颜色之间切换 vueJS
【发布时间】:2018-11-06 20:22:50
【问题描述】:

这是我从今天开始的上一个问题的后续:getElementsByClassName in context of vue

我现在想修改代码,以便每次单击时都会在两种颜色之间切换。

我的第一次尝试是这样的:

<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
        <style>
            .main-header {
                color: #292c2e;
            }
        </style>
    </head>
    <body>
    <div id="app">
        <h1 class="main-header" v-bind:style="{color: clickedColor}">{{ message }}</h1>
        <button v-on:click="colorChange">Click me</button>
    </div>
    <script>
        var app = new Vue({
        el: '#app',
            data: {
                message: 'Hello Vue!',
                clickedColor: '',
                alteredState: false
            },
            methods: {
                colorChange: function() {
                    console.log(this.alteredState);
                    this.alteredState = true;
                    if (this.alteredState == true) {
                        this.clickedColor = 'green'
                        this.alteredState = false;
                    } else {
                        this.clickedColor = ''
                    }
                }
            }
        })
    </script>
    </body>
</html>

基本上我在这里的思考过程......我正在做(尝试)的是我有一个名为alteredState的第二个数据集 - 我最初将它设置为false。在更改颜色功能中,我将其设置为 true。将其设置为 true 后,我检查它是否为 true。如果是这样,请更改颜色并将其恢复为 false。我的期望是在第二次点击时,它会恢复为主要颜色,然后在第二次点击时它会恢复为绿色。

最终发生的是它变为绿色,然后无限期设置为 false 并且不会变回黑色。

怎么没用?

然后我了解了道具https://vuejs.org/v2/guide/components-props.html 并尝试了类似的方法(这似乎是朝着正确方向迈出的一步):

<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
        <style>
            .main-header {
                color: #292c2e;
            }
        </style>
    </head>
    <body>
    <div id="app">
        <h1 class="main-header" v-bind:style="{color: clickedColor}">{{ message }}</h1>
        <button v-on:click="colorChange">Click me</button>
    </div>
    <script>
        var app = new Vue({
        el: '#app',
            data: {
                message: 'Hello Vue!',
                clickedColor: ''
            },
            state: {
                alteredState: 'false'
            },
            methods: {
                colorChange: function() {
                    this.clickedColor = 'green'
                    this.alteredState.setState = 'true'
                }
            }
        })
    </script>
    </body>
</html>

但我不明白如何更改状态的值,因为 Vue 文档并没有真正解释它。

单击按钮时在标题的两种不同颜色之间切换是最好的方法吗?或者我是否更接近尝试 #1 的 ifs 等?

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    尝试在条件块之前删除this.alteredState = true;,并通过单击同时更改alteredState 值的按钮在黑色和绿色之间切换:

    <html>
        <head>
            <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
            <style>
                .main-header {
                    color: #292c2e;
                }
            </style>
        </head>
        <body>
        <div id="app">
            <h1 class="main-header" v-bind:style="{color: clickedColor}">{{ message }}</h1>
            <button v-on:click="colorChange">Click me</button>
        </div>
        <script>
            var app = new Vue({
            el: '#app',
                data: {
                    message: 'Hello Vue!',
                    clickedColor: '',
                    alteredState: false
                },
                methods: {
                    colorChange: function() {
                        console.log(this.alteredState);
                     
                        if (this.alteredState ) {
                            this.clickedColor = 'green'
                            this.alteredState = false;
                        } else {
                            this.clickedColor = '#000'
                             this.alteredState = true;
                        }
                    }
                }
            })
        </script>
        </body>
    </html>

    【讨论】:

      【解决方案2】:

      您将alteredState 设置为真,然后立即检查它是否为真——所以它始终为真。

      你的alteredState 变量根本没有理由;您可以根据当前颜色进行切换:

      colorChange: function() {
        if (this.clickedColor == 'green') {
          this.clickedColor = '';
        } else {
          this.clickedColor = 'green'
        }
      }
      

      但如果你真的想要那个alteredState var,你会想根据切换状态来设置它,而不是每次都设置它为真:

          colorChange: function() {
            if (this.alteredState) {
              this.clickedColor = 'green'
              this.alteredState = false;
            } else {
              this.clickedColor = ''
              this.alteredState = true;
            }
          }
      

      还要注意true(布尔值)和'true'(字符串)之间的区别。

      【讨论】:

        【解决方案3】:

        我认为 @Boussadjra Brahim 的回答还不错,但我会稍微清理一下,并消除最初的双击更改颜色问题。 p>

        var app = new Vue({
          el: '#app',
          data: {
            message: 'Hello Vue!',
            clickedColor: 'red', // set the initial value; I used red, so you can see the different states
            alteredState: true
          },
          methods: {
            colorChange: function() {
              console.log(this.alteredState);
        
              if (this.alteredState) {
                this.clickedColor = 'green'
              } else {
                this.clickedColor = '#000'
              }
              // this is toggle function, so it's OK to
              // toggle the state every time the button is clicked
              this.alteredState = !this.alteredState
            }
          }
        })
        /* you don't need this CSS, as you set the color with :style */
        
        
        /*.main-header {
            color: #292c2e;
        */
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
        
        <div id="app">
          <h1 class="main-header" :style="{color: clickedColor}">{{ message }}</h1>
          <button v-on:click="colorChange">Click me</button>
        </div>

        希望这对您有所帮助! :)

        【讨论】:

          【解决方案4】:

          答案迟了,但会帮助其他人实现完美的切换按钮。

          new Vue({
            el: '#app',
            props: {
              disabled: {
                type: Boolean,
                default: false
              },
              labelEnableText: {
                type: String,
                default: 'On'
              },
              labelDisableText: {
                type: String,
                default: 'Off'
              },
              id: {
                type: String,
                default: 'primary'
              },
              defaultState: {
                type: Boolean,
                default: false
              }
            },
            data() {
              return {
                currentState: this.defaultState
              }
            },
          
            computed: {
              enableText() {
                return this.labelEnableText;
              },
          
              disableText() {
                return this.labelDisableText;
              },
          
              isActive() {
                return this.currentState;
              },
          
              checkedValue: {
                get() {
                  return this.currentState
                },
                set(newValue) {
                  console.log(newValue);
                  this.currentState = newValue;
                }
              }
            }
          });
          .toggle__button {
              vertical-align: middle;
              user-select: none;
              cursor: pointer;
          }
          .toggle__button input[type="checkbox"] {
              opacity: 0;
              position: absolute;
              width: 1px;
              height: 1px;
          }
          .toggle__button .toggle__switch {
              display:inline-block;
              height:12px;
              border-radius:6px;
              width:40px;
              background: #BFCBD9;
              box-shadow: inset 0 0 1px #BFCBD9;
              position:relative;
              margin-left: 10px;
              transition: all .25s;
          }
          
          .toggle__button .toggle__switch::after, 
          .toggle__button .toggle__switch::before {
              content: "";
              position: absolute;
              display: block;
              height: 18px;
              width: 18px;
              border-radius: 50%;
              left: 0;
              top: -3px;
              transform: translateX(0);
              transition: all .25s cubic-bezier(.5, -.6, .5, 1.6);
          }
          
          .toggle__button .toggle__switch::after {
              background: #4D4D4D;
              box-shadow: 0 0 1px #666;
          }
          .toggle__button .toggle__switch::before {
              background: #4D4D4D;
              box-shadow: 0 0 0 3px rgba(0,0,0,0.1);
              opacity:0;
          }
          
          .active .toggle__switch {
              background: #adedcb;
              box-shadow: inset 0 0 1px #adedcb;
          }
          
          .active .toggle__switch::after,
          .active .toggle__switch::before{
              transform:translateX(40px - 18px);
          }
          
          .active .toggle__switch::after {
              left: 23px;
              background: #53B883;
              box-shadow: 0 0 1px #53B883;
          }
          <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
          <div id="app">
            <p><strong>Basic Example</strong></p>
            <label :for="id + '_button'" :class="{'active': isActive}" class="toggle__button">
              <span v-if="isActive" class="toggle__label" v-text="enableText"></span>
              <span v-if="! isActive" class="toggle__label" v-text="disableText"></span>
          
              <input type="checkbox" :disabled="disabled" :id="id + '_button'" v-model="checkedValue">
              <span class="toggle__switch"></span>
            </label>
          </div>

          查看更多详情https://webomnizz.com/create-toggle-switch-button-with-vue-js/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-07-06
            • 1970-01-01
            • 1970-01-01
            • 2012-11-27
            • 2018-07-24
            • 1970-01-01
            • 2018-10-26
            相关资源
            最近更新 更多