vue.js從入門到深入再到隨心而用———利用所學的基本知識製作點擊對應的標識下方圖片跟隨變動的效果

利用所學的基本知識製作點擊對應的標識下方圖片跟隨變動的效果

1.效果

在這裏插入圖片描述

點擊圖片上方的car1,car2,car3下方的圖片隨着變動

2.代碼的實現

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		  <style type="text/css">
    
    .tab ul {
      overflow: hidden;
      padding: 0;
      margin: 0;
    }
    .tab ul li {
      box-sizing: border-box;
      padding: 0;
      float: left;
      width: 100px;
      height: 45px;
      line-height: 45px;
      list-style: none;
      text-align: center;
      border-top: 1px solid blue;
      border-right: 1px solid blue;
      cursor
    }
    .tab ul li:first-child {
      border-left: 1px solid blue;
    }
    .tab ul li.active {
      background-color: orange;
    }
    .tab div {
      width: 500px;
      height: 300px;
      display: none;
      text-align: center;
      font-size: 30px;
      line-height: 300px;
      border: 1px solid blue;
      border-top: 0px;
    }
    .tab div.current {
      display: block;
    }
    img{
    	width: 800px;
    	height: 500px;
    }
  </style>
		<script type="text/javascript" src="../js/vue.min.js" ></script>
	</head>
	<body>
		<div id="app">
			<div class="tab">
				<ul>
					<li @click='change(index)' :class='currentIndex==index?"active":""' :key='item.id' v-for="(item,index) in list">{{item.title}}</li>
				</ul>
				<div :class='currentIndex==index?"current":""' :key='item.id' v-for='(item, index) in list'>
					<img :src="item.path"/>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			
		new Vue({
			el:'#app',
			data:{
				currentIndex: 0, // 選項卡當前的索引
				list:[{
					id:1,
					title:'car1',
					path:'img/car1.jpg'
				},{
					id:2,
					title:'car2',
					path:'img/car2.jpg',
				},{
					id:3,
					title:'car3',
					path:'img/car3.jpg'
				}]
			},
			methods:{
				change:function(index){
					this.currentIndex=index;
				}
			}
		})
		</script>

	</body>
</html>

3.注意的點:

設值選項卡的索引爲currentIndex,然後每次點擊讓currentIndex的值=點擊圖片的index,這樣就實現了點擊的時候對應的樣式改變同時圖片也發生變化
要把問題進行分析,通過綁定或者事件給聯繫起來

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章