- 時間ボタンを押す
- そのボタンのvalueを取得する
- 同じ文字列を持つimg要素のshowをtrueにし、それ以外を全てfalseにする
上記の機能を実装したいのですが、
0. methodsに引数を渡す、
0. その値をimg要素が持つ文字列と照合する、
0. 対応した文字列を持つimgのshowをtrueにする
という部分が調べてもわからず質問させていただきました。
以下コードになります。
HTML
1 <div id="app"> 2 <div> 3 <div id="imgArea"> 4 <img :src="itemsDire[index].src" :alt="itemsDire[index].alt" class="imgCanvas"> 5 <div v-show="showA"> 6 <div v-for="item in itemsA" :val="item.value"> 7 <img :key="item.src" :src="item.src" :alt="item.alt" v-show="showShadow" class="imgCanvas" :class="item.class"> 8 </div> 9 </div> 10 <div v-show="showB"> 11 <div v-for="item in itemsB" :val="item.value"> 12 <img :key="item.src" :src="item.src" :alt="item.alt" v-show="showShadow" class="imgCanvas" :class="item.class"> 13 </div> 14 </div> 15 </div> 16 <div id="btnArea"> 17 <div id="directionBtnArea"> 18 <input type="button" @click="changeNW" value="NW"> 19 <input type="button" @click="changeNE" value="NE"> 20 <input type="button" @click="changeSE" value="SE"> 21 <input type="button" @click="changeSW" value="SW"> 22 </div> 23 <div id="shadowBtnArea"> 24 <input type="button" v-for="item in itemTimes" @click="changeShadow" :value="item" :class="item"> 25 </div> 26 <div id="seasonBtnArea"> 27 <input type="button" @click="showA=!showA" value="A"> 28 <input type="button" @click="showB=!showB" value="B"> 29 </div> 30 </div> 31 </div> 32 </div>
Vue.js
1new Vue ({ 2 el: '#app', 3 data: { 4 itemsDire: [], 5 itemsA: [], 6 itemsB: [], 7 itemTimes:['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00'], 8 index: 0, 9 showA: true, 10 showB: false, 11 showShadow: true 12 }, 13 methods: { 14 changeNW: function() { 15 axios.get('json/NW.json').then(function(response) { 16 console.log(response) 17 this.itemsDire = response.data[0].imageDire; 18 this.itemsA = response.data[0].imageA; 19 this.itemsB = response.data[0].imageB; 20 }.bind(this)).catch(function(e) { 21 console.error('error!') 22 }) 23 }, 24 changeNE: function() { 25 axios.get('json/NE.json').then(function(response) { 26 console.log(response) 27 this.itemsDire = response.data[0].imageDire; 28 this.itemsA = response.data[0].imageA; 29 this.itemsB = response.data[0].imageB; 30 }.bind(this)).catch(function(e) { 31 console.error('error!') 32 }) 33 }, 34 changeSE: function() { 35 axios.get('json/SE.json').then(function(response) { 36 console.log(response) 37 this.itemsDire = response.data[0].imageDire; 38 this.itemsA = response.data[0].imageA; 39 this.itemsB = response.data[0].imageB; 40 }.bind(this)).catch(function(e) { 41 console.error('error!') 42 }) 43 }, 44 changeSW: function() { 45 axios.get('json/SW.json').then(function(response) { 46 console.log(response) 47 this.itemsDire = response.data[0].imageDire; 48 this.itemsA = response.data[0].imageA; 49 this.itemsB = response.data[0].imageB; 50 }.bind(this)).catch(function(e) { 51 console.error('error!') 52 }) 53 }, 54 changeShadow: function() { 55 this.value = event.target.value; 56 console.log(this.value); 57 } 58 }, 59 created: function() { 60 axios.get('json/NW.json').then(function(response) { 61 console.log(response) 62 this.itemsDire = response.data[0].imageDire; 63 this.itemsA = response.data[0].imageA; 64 this.itemsB = response.data[0].imageB; 65 }.bind(this)).catch(function(e) { 66 console.error('error!') 67 }) 68 } 69})
追記
先のコードだとわかりにくいと思い修正をさせていただきました。
まず土台となる画像(itemDire)を表示し、季節の選択(AかBか)、その後時間を指定して影を表示させるという流れになっております。
全ての画像はaxiosを使い、jsonファイルでdata内のそれぞれの配列に入れています。
現状はevent.target.valueでボタンに設定したvalue(時間)を取得できましたが、その後どうやってdata内部の配列(itemAとitemB)と比較をしshowを切り替えるかという部分です。
このままmethodsのみで処理できるのか、computedを使うのか、componentで受け渡しをした方がスマートなのかわからずに困っております。
ご回答よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー