ソースを参照

HW YB2NKR8B2LL done

Varvara Huza 3 年 前
コミット
2747bf2593
4 ファイル変更340 行追加23 行削除
  1. 22 23
      Homework_10/main.js
  2. BIN
      Homework_11/1@3x.png
  3. 74 0
      Homework_11/index.html
  4. 244 0
      Homework_11/main.js

+ 22 - 23
Homework_10/main.js

@@ -42,19 +42,19 @@ for (let i = 0; i < multiplyTable.length; i++) {
 tableContainer.appendChild(table)
 
 //подсветка строки и столбца с замыканиями
-let cells = document.querySelectorAll('td');
 let rows = document.querySelectorAll('tr');
 
-function highlightRow (tr) {
-    let row = tr.children;
-    function highlightCell (td, tdCount) {
+for (let tr of rows) {
+    let tdCounter = 0;
+    for (let td of tr.children) {
+        let tdCounterToRemember = tdCounter;
         td.onmouseover = function () {
             for (let column of rows) {
-                column.children[tdCount].style.backgroundColor = '#bcf'
-                column.children[tdCount].style.color = '#fff'
+                column.children[tdCounterToRemember].style.backgroundColor = '#bcf'
+                column.children[tdCounterToRemember].style.color = '#fff'
             }
 
-            for (let rowCell of row) {
+            for (let rowCell of tr.children) {
                 rowCell.style.backgroundColor = '#bcf'
                 rowCell.style.color = '#fff'
             }
@@ -63,26 +63,17 @@ function highlightRow (tr) {
         }
         td.onmouseout = function () {
             for (let column of rows) {
-                column.children[tdCount].style.backgroundColor = ''
-                column.children[tdCount].style.color = ''
+                column.children[tdCounterToRemember].style.backgroundColor = ''
+                column.children[tdCounterToRemember].style.color = ''
             }
 
-            for (let rowCell of row) {
+            for (let rowCell of tr.children) {
                 rowCell.style.backgroundColor = ''
                 rowCell.style.color = ''
             }
             td.style.backgroundColor = ''
             td.style.color = ''
         }
-    }
-    return highlightCell;
-}
-
-for (let row of rows) {
-    let highlightCell = highlightRow(row);
-    let tdCounter = 0;
-    for (let cell of row.children) {
-        highlightCell(cell, tdCounter);
         tdCounter++
     }
 }
@@ -103,7 +94,7 @@ function makeProfileTimer () {
 function makeSaver (func) {
     let result;
     function returnResult () {
-        if (!result) {
+        if (result === undefined) {
             result = func()
         }
         return result;
@@ -125,6 +116,16 @@ function countdown (numb) {
         }, 1000)
 }
 
+//or
+// let n = 5
+// for (let i = n, j = 1; i >= 0; i--, j++) {
+//     if (i === 0) {
+//         setTimeout(console.log, 1000 * j, 'Поехали!')
+//         break
+//     }
+//     setTimeout(console.log, 1000 * j, i)
+// }
+
 //MyBind
     function myBind(functionToBind, context, args) {
         function boundFunction (...newArgs) {
@@ -140,9 +141,7 @@ function countdown (numb) {
                 }
             }
             if (context) {
-                context.key = functionToBind
-                functionResult = context.key(...commonArgs)
-                delete context.key
+                functionResult = functionToBind.call(context, ...commonArgs)
             } else {
                 functionResult = functionToBind(...commonArgs)
             }

BIN
Homework_11/1@3x.png


+ 74 - 0
Homework_11/index.html

@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <style>
+        .container {
+            max-width: 800px;
+            margin: 15px auto;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+        }
+
+        #background {
+            height: 300px;
+        }
+
+        #regulatorContainer img {
+            width: 125px;
+        }
+
+        img {
+            width: 175px;
+        }
+
+        #btn {
+            width: 0;
+            height: 0;
+            border: 45px solid #000;
+            border-right: none;
+            border-top-color: transparent;
+            border-bottom-color: transparent;
+            background-color: #fff;
+            padding: 0;
+            transition: 0.3s ease-in;
+            cursor: pointer;
+        }
+
+        #btn:hover {
+            transform: scale(1.2)
+        }
+
+        #formContainer {
+            max-width: 250px;
+            flex-direction: column;
+            align-items: flex-start;
+        }
+
+        #formContainer div {
+            margin-bottom: 15px;
+        }
+
+    </style>
+</head>
+<body>
+
+    <div class="container" id="regulators"></div>
+    <div class="container" id="background"></div>
+
+    <div class="container" id="regulatorContainer">
+        <button id='btn'></button>
+        <audio id="audio">
+            <source src="https://vzvuke.net/play/MDE0MWViMGNjZGFiODQzYmRlNjJkMGE2ZDAwNzEyOWYubXAz/">
+        </audio>
+    </div>
+
+    <div class="container" id="formContainer"></div>
+    
+    <script src="main.js"></script>
+</body>
+</html>

+ 244 - 0
Homework_11/main.js

@@ -0,0 +1,244 @@
+function Control(element, {value=0, min=0, max=100, minAngle=0, maxAngle=360, wheelSpeed=0.1, step=1}={}) {
+
+    const container = document.createElement('div')
+    container.style.padding = '15px'
+    const img = document.createElement('img')
+    const regulatorValue = document.createElement('div')
+    regulatorValue.style.textAlign = 'center'
+    img.src = '1@3x.png'
+    container.append(img)
+    container.append(regulatorValue)
+    element.append(container)
+
+    const ratio = (maxAngle - minAngle) / (max - min)
+    const getAngle = () => (ratio * (value - min)) + minAngle
+
+    this.setValue = newValue => {
+        if (newValue > max) {
+            newValue = max
+        }
+        if (newValue < min) {
+            newValue = min
+        }
+        value = newValue
+        img.style.transform = `rotate(${getAngle()}deg)`
+        regulatorValue.innerText = value
+
+        if (typeof this.onChange === 'function') {
+            this.onChange(value)
+        }
+    }
+
+    this.getValue = () => {
+        return value
+    }
+
+    img.onmousewheel = (e) => {
+        e.preventDefault()
+        let {deltaY} = e
+        this.setValue(value + deltaY * wheelSpeed)
+    }
+
+    img.onclick = (e) => {
+        let {layerX} = e
+        if (layerX < img.width / 2) {
+            this.setValue(value - step)
+        }
+        if (layerX > img.width / 2) {
+            this.setValue(value + step)
+        }
+    }
+
+    this.setValue(value)
+}
+
+//крутилки цвета
+const rControl = new Control(regulators, {value: 0, min: 0, max: 255, minAngle: -90, maxAngle: +90})
+const gControl = new Control(regulators, {value: 0, min: 0, max: 255, minAngle: -90, maxAngle: +90})
+const bControl = new Control(regulators, {value: 0, min: 0, max: 255, minAngle: -90, maxAngle: +90})
+
+function changeBackgroundColor() {
+    this.style.backgroundColor = `rgb(${rControl.getValue()}, ${gControl.getValue()}, ${bControl.getValue()})`
+}
+
+const changeDivBackground = changeBackgroundColor.bind(background)
+changeDivBackground()
+rControl.onChange = changeDivBackground
+gControl.onChange = changeDivBackground
+bControl.onChange = changeDivBackground
+
+//крутилка звука
+//я прошарила, что тут можно мин макс поменять и скорость колесика, и тогда можно не делить на 100, но если так сделать, то число некрасивое отображается, мне не нравится......
+const volumeControl = new Control(regulatorContainer, {value: 50, min: 0, max: 100, minAngle: -90, maxAngle: +90, wheelSpeed: 0.05})
+
+function changeVolume () {
+    audio.volume = volumeControl.getValue() / 100
+}
+volumeControl.onChange = changeVolume
+
+btn.onclick = () => audio.play()
+
+//Password
+function Password(parent, open){
+    let container = document.createElement('div')
+    container.style.display = 'flex'
+    container.style.alignItems = 'center'
+
+    let passwordInput = document.createElement('input')
+    passwordInput.type = open ? 'text' : 'password'
+
+    let openBtn = document.createElement('span')
+    openBtn.style.lineHeight = '0'
+    openBtn.innerHTML = '&#128065;'
+    openBtn.style.fontSize = '25px'
+    openBtn.style.cursor = 'pointer'
+
+    container.append(passwordInput)
+    container.append(openBtn)
+    parent.appendChild(container)
+
+    let value = passwordInput.value
+    this.setValue = (newValue) => {
+        value = newValue
+        passwordInput.value = value;
+        return value
+    }
+    this.getValue = () => {
+        return value
+    }
+
+    this.setOpen = (newValue) => {
+        if (newValue === true || newValue === false) {
+            open = newValue
+            passwordInput.type = open ? 'text' : 'password'
+        }
+        return open
+    }
+    this.getOpen = () => {
+        return open
+    }
+
+    passwordInput.oninput = () => {
+        this.setValue(passwordInput.value)
+        if (typeof this.onChange === 'function') {
+            this.onChange(value)
+        }
+    }
+
+    openBtn.onclick = () => {
+        open = !open
+        this.setOpen(open)
+        if (typeof this.onOpenChange === 'function') {
+            this.onOpenChange(open)
+        }
+    }
+}
+
+//LoginForm
+// let container = document.createElement('div')
+// let loginInput = document.createElement('input')
+// container.append(loginInput)
+// formContainer.append(container)
+
+// let passwordInput = new Password(formContainer, true)
+
+// let logInBtn = document.createElement('button')
+// logInBtn.innerText = 'Log in'
+// formContainer.append(logInBtn)
+
+// loginInput.oninput = inputCheck
+// passwordInput.onChange = inputCheck
+// function inputCheck() {
+//     if (loginInput.value !== '' && passwordInput.getValue() !== '') {
+//         logInBtn.disabled = false
+//     } else {
+//         logInBtn.disabled = true
+//     }
+// }
+// inputCheck()
+
+//LoginForm Constructor
+function LoginForm (parent) {
+    let container = document.createElement('div')
+    let loginInput = document.createElement('input')
+    container.append(loginInput)
+    parent.append(container)
+
+    let passwordInput = new Password(parent, true)
+
+    let logInBtn = document.createElement('button')
+    logInBtn.innerText = 'Log in'
+    formContainer.append(logInBtn)
+
+    let value = loginInput.value
+    this.setValue = (newValue) => {
+        value = newValue
+        loginInput.value = value;
+        return value
+    }
+    this.getValue = () => {
+        return value
+    }
+
+    this.getPasswordInput = () => {
+        return passwordInput
+    }
+
+    this.inputCheck = () => {
+        if (value !== '' && passwordInput.getValue() !== '') {
+            logInBtn.disabled = false
+        } else {
+            logInBtn.disabled = true
+        }
+    }
+    this.inputCheck()
+
+    loginInput.oninput = () => {
+        this.setValue(loginInput.value)
+        this.inputCheck()
+        if (typeof this.onChange === 'function') {
+            this.onChange(value)
+        }
+    }
+    passwordInput.onChange = this.inputCheck
+}
+
+//Password Verify
+//надеюсь, у вас не случится сердечный приступ от количества костылей в этом коде
+let password = new Password(formContainer, true)
+let passwordVerify
+let logInBtn = document.createElement('button')
+logInBtn.innerText = 'Verify'
+formContainer.append(logInBtn)
+
+password.onOpenChange = (open) => {
+    password.onChange()
+    if (!open) {
+        passwordVerify = new Password(formContainer, false)
+        passwordVerify.onChange = password.onChange
+        passwordVerify.onChange()
+        formContainer.append(logInBtn)
+    } else if (formContainer.children.length === 3){
+        passwordVerify = undefined
+        formContainer.removeChild(formContainer.children[formContainer.children.length - 2])
+        
+    }
+}
+
+password.onChange = () => {
+    if (passwordVerify !== undefined) {
+        if (password.getValue() === passwordVerify.getValue() && password.getValue() !== '') {
+            logInBtn.disabled = false
+        } else {
+            logInBtn.disabled = true
+        }
+    } else {
+        if (password.getValue() !== '') {
+            logInBtn.disabled = false
+        } else {
+            logInBtn.disabled = true
+        }
+    }
+}
+
+password.onOpenChange(password.getOpen())