Visitar URL original
10 - shift selects checkbox in middle · wesbos/JavaScript30@edc8fbb · GitHub
Skip to content

Commit edc8fbb

Browse files
committed
10 - shift selects checkbox in middle
1 parent 0f89a41 commit edc8fbb

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎src/08 - Fun with HTML5 Canvas/index-START.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ document.addEventListener('DOMContentLoaded', () => {
33
const ctx = canvas.getContext('2d')
44

55
let isDrawing = false
6-
let currentColor = '#000'
6+
let currentColors = []
7+
let strokeIndex = 0
78
let lastX = 0
89
let lastY = 0
910
let lineWidth = 1
@@ -72,29 +73,30 @@ document.addEventListener('DOMContentLoaded', () => {
7273
canvas.addEventListener('mousedown', (ev) => {
7374
ev.preventDefault()
7475
isDrawing = true
75-
currentColor = '#222'
76+
currentColors = ['#000', '#222', '#444', '#666']
7677
_saveLastPosition(ev)
7778
})
7879

7980
canvas.addEventListener('contextmenu', (ev) => {
8081
ev.preventDefault()
8182
isDrawing = true
82-
currentColor = '#1d41d6'
83+
currentColors = ['#001867', '#1d41d6']
8384
_saveLastPosition(ev)
8485
})
8586
}
8687

87-
function _drawLine(ev) {
88+
function _drawLine (ev) {
8889
if (isDrawing) {
89-
ctx.strokeStyle = currentColor
90+
ctx.strokeStyle = currentColors[strokeIndex % currentColors.length]
9091
ctx.lineCap = 'round'
9192
ctx.lineJoin = 'round'
92-
ctx.lineWidth = lineWidth
93+
ctx.lineWidth = Math.floor(Math.random() * 2) + lineWidth
9394
ctx.beginPath()
9495
ctx.moveTo(lastX, lastY)
9596
ctx.lineTo(ev.offsetX, ev.offsetY)
9697
ctx.stroke()
97-
ctx.strokeStyle.bold()
98+
99+
strokeIndex++
98100
}
99101
}
100102

‎src/10 - Hold Shift and Check Checkboxes/index-START.html‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
</div>
104104
</div>
105105

106-
<script>
107-
</script>
106+
<script src="index-START.js"></script>
108107
</body>
109108
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const checkboxes = document.querySelectorAll('input')
2+
let lastIndex = -1
3+
checkboxes.forEach((checkbox, index) => {
4+
checkbox.dataset.index = index
5+
checkbox.addEventListener('click', (ev) => {
6+
const currentIndex = ev.target.dataset.index
7+
const currentCheckedState = ev.target.checked
8+
if (ev.shiftKey) {
9+
if (lastIndex >= 0) {
10+
if (lastIndex < currentIndex) {
11+
for (let i = lastIndex; i <= currentIndex; i++) {
12+
const currentElement = document.querySelector(`input[data-index="${i}"]`)
13+
currentElement.checked = currentCheckedState
14+
}
15+
} else {
16+
for (let i = lastIndex; i >= currentIndex; i--) {
17+
const currentElement = document.querySelector(`input[data-index="${i}"]`)
18+
currentElement.checked = currentCheckedState
19+
}
20+
}
21+
}
22+
} else {
23+
lastIndex = currentIndex
24+
console.log(`--lastIndex--`); console.log(lastIndex) // DEBUG
25+
}
26+
})
27+
})

0 commit comments

Comments
 (0)