Skip to content

Commit 87d6dd3

Browse files
committed
selectedVariant
1 parent 968668b commit 87d6dd3

File tree

3 files changed

+55
-56
lines changed

3 files changed

+55
-56
lines changed

01-HelloWorld/01-Socks.html

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,50 @@
1616
</div>
1717

1818
<div class="product-info">
19-
<!-- Binding -->
20-
<h1>{{ product }}</h1>
19+
<!-- Binding -->
20+
<h1>{{ product }}</h1>
2121

2222

23-
<!-- Conditionals -->
24-
<div>
25-
<!-- Falsey will not include in DOM -->
26-
<p v-if="inventory > 10">In Stock</p>
27-
<p v-else-if="inventory">Almost sold out</p>
28-
<p v-else>Out of Stock</p>
23+
<!-- Conditionals -->
24+
<div>
25+
<!-- Falsey will not include in DOM -->
26+
<p v-if="inventory > 10">In Stock</p>
27+
<p v-else-if="inventory">Almost sold out</p>
28+
<p v-else>Out of Stock</p>
2929

30-
<!-- Render with display: none -->
31-
<p v-show="inventory < 0">Wuuk?</p>
32-
</div>
30+
<!-- Render with display: none -->
31+
<p v-show="inventory < 0">Wuuk?</p>
32+
</div>
3333

3434

35-
<!-- Events -->
36-
<button
37-
v-on:click="cart++; inventory--"
38-
:disabled="!inventory"
39-
:class="{disabledButton: inventory ? false : true}"
40-
>
41-
<!-- Method: addToCart -->
42-
Add to Cart
43-
</button>
35+
<!-- Events -->
36+
<button
37+
v-on:click="cart++; inventory--"
38+
:disabled="!inventory"
39+
:class="{disabledButton: inventory ? false : true}"
40+
>
41+
<!-- Method: addToCart -->
42+
Add to Cart
43+
</button>
4444

45-
<div class="cart">
46-
{{cart}}
47-
<i class="fas fa-shopping-cart"></i>
48-
</div>
45+
<div class="cart">
46+
{{cart}}
47+
<i class="fas fa-shopping-cart"></i>
48+
</div>
4949

5050

51-
<!-- Loops -->
52-
<div v-for="variant in variants"
53-
:key="variant.id"
54-
class="color-box"
55-
:style="{backgroundColor: variant.color}"
56-
@mouseover="setImage(variant.color)">
57-
</div>
51+
<!-- Loops -->
52+
<div v-for="variant in variants"
53+
:key="variant.id"
54+
class="color-box"
55+
:style="{backgroundColor: variant.color}"
56+
@mouseover="setImage(variant.color)">
57+
</div>
5858
</div>
5959
</div>
6060
</div>
6161

62-
<a href="02-FancierSocks.html">
62+
<a href="02-FancierSocks.html" class="next">
6363
moving on <i class="fas fa-arrow-right"></i>
6464
</a>
6565
</body>

01-HelloWorld/02-FancierSocks.html

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,18 @@
1717
<h1>{{ title }}</h1>
1818

1919

20-
<!-- Conditionals -->
2120
<div>
22-
<!-- Falsey will not include in DOM -->
2321
<p v-if="inventory > 10">In Stock</p>
2422
<p v-else-if="inventory">Almost sold out</p>
2523
<p v-else>Out of Stock</p>
26-
27-
<!-- Render with display: none -->
28-
<p v-show="inventory < 0">Wuuk?</p>
2924
</div>
3025

3126

32-
<!-- Events -->
3327
<button
3428
v-on:click="cart++; inventory--"
3529
:disabled="!inventory"
36-
:class="{disabledButton: inventory ? false : true}"
30+
:class="inventory ? '' : 'disabledButton'"
3731
>
38-
<!-- Method: addToCart -->
3932
Add to Cart
4033
</button>
4134

@@ -46,15 +39,19 @@ <h1>{{ title }}</h1>
4639

4740

4841
<!-- Loops -->
49-
<div v-for="variant in variants"
50-
:key="variant.id"
51-
class="color-box"
52-
:style="{backgroundColor: variant.color}"
53-
@mouseover="setImage(variant.color)">
54-
</div>
42+
<div v-for="(variant, index) in variants"
43+
:key="variant.id"
44+
class="color-box"
45+
:style="{backgroundColor: variant.color}"
46+
@mouseover="setSelectedVariant(variant, index)">
47+
</div>
5548
</div>
5649
</div>
5750
</div>
51+
52+
<a href="01-Socks.html" class="prev">
53+
<i class="fas fa-arrow-left"></i> moving back
54+
</a>
5855
</body>
5956
</html>
6057

@@ -64,8 +61,7 @@ <h1>{{ title }}</h1>
6461
el: '#app',
6562
data: {
6663
product: 'Socks',
67-
image: './assets/socks-green.jpg',
68-
imageTitle: 'Green Socks',
64+
selectedVariantIndex: 0,
6965
variants: [
7066
{id: 1, color: 'green'},
7167
{id: 2, color: 'blue'}
@@ -74,19 +70,19 @@ <h1>{{ title }}</h1>
7470
cart: 0
7571
},
7672
methods: {
77-
addToCart() {
78-
this.cart++;
79-
this.inventory--;
80-
},
81-
setImage(color) {
82-
console.log('Vue object with data and methods', this);
83-
this.image = `./assets/socks-${color}.jpg`;
73+
setSelectedVariant(variant, index) {
74+
console.log('hover', index, variant);
75+
this.selectedVariantIndex = index;
8476
}
8577
},
8678
computed: {
8779
title() {
8880
// Return values are cached
8981
return this.product.toUpperCase();
82+
},
83+
image() {
84+
const color = this.variants[this.selectedVariantIndex].color;
85+
return `./assets/socks-${color}.jpg`;
9086
}
9187
}
9288
});

01-HelloWorld/assets/style.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ textarea {
9898
a {
9999
text-decoration: none;
100100
color: black;
101+
margin: 0 12px;
102+
}
103+
104+
a.next {
101105
float: right;
102-
margin-right: 12px;
103106
}

0 commit comments

Comments
 (0)