Падающие листья со снегом на HTML5 + CSS3 + jQuery
Интересный эффект падающих листьев вперемешку со снежинками. Актуально в октябре
Для начала посмотрите ДЕМО
Установка:
1: В самый низ вашего CSS вставьте:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
.leaf0, .leaf1{ position: fixed; background: url("/img/Black_Leaf0.png") no-repeat; background-size: contain; z-index: -500; display: none; -webkit-transform: translate3d(0, 0, 0); -webkit-backface-visibility: hidden; } .leaf1 { position: fixed; background: url("/img/Black_Leaf1.png") no-repeat; background-size: contain; z-index: -500; display: none; -webkit-transform: translate3d(0, 0, 0); -webkit-backface-visibility: hidden; } .snowflake0 { background: url("/img/Snowflake0.png") no-repeat; position: fixed; width: 10px; height: 10px; background-size: contain; z-index: -500; -webkit-transform: translate3d(0, 0, 0); -webkit-backface-visibility: hidden; } .tree { visibility: hidden; position: fixed; top: 15vh; left: 2vw; height: 25vh; width: 22vw; background: red; } .sky { visibility: hidden; position: fixed; top: 0; left: 0; height: 10vh; width: 100vw; background: skyblue; } |
2#: После открывающего тега <body> вствьте:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
<script type="text/javascript"> window.addEventListener('load', function () { let numLeaves = 60; let numSnow = 50; let curLeaves = 0; let curSnow = 0; let didScroll = false; function scrolled() { didScroll = true; window.removeEventListener("scroll", scrolled); } window.addEventListener("scroll", scrolled); let wait = 800; setInterval(function() { if(didScroll) { didScroll = false; let cur = (document.documentElement.scrollTop || document.body.scrollTop); let loc = cur / ((document.documentElement.scrollHeight || document.body.scrollHeight) - window.innerHeight) * 100; if (loc < 25) { numLeaves = 60; numSnow = 0; wait = 800; } else if (loc < 50) { numLeaves = 100; numSnow = 20; wait = 500; } else if (loc < 75) { numLeaves = 20; numSnow = 110; wait = 300; } else { numLeaves = 35; numSnow = 25; wait = 1000; } window.addEventListener("scroll", scrolled); } }, 750); /* PROCEDURE TO CONTROL BACKGROUND ANIMATIONS */ let height = document.documentElement.clientHeight; let width = Math.max( document.documentElement.clientWidth, document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth ); window.addEventListener("resize", function () { height = document.documentElement.clientHeight; width = Math.max( document.documentElement.clientWidth, document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth ); }); let dir = []; let speed = []; let grav = []; let wind = []; let rot = []; let delta = []; function createChild(parent, childName) { let bound = parent.getBoundingClientRect(); let startX = ((Math.random() * (bound.right - bound.left)) + bound.left) + "px"; let startY = ((Math.random() * (bound.bottom - bound.top)) + bound.top) + "px"; let newChild = document.createElement("div"); newChild.style.top = startY; newChild.style.left = startX; newChild.style.transform = randomRotate(); let z = newChild.style.transform.split(" ")[2].replace("rotateZ(", "").replace("deg)", ""); grav.push(Math.random() / 6 + 0.3); speed.push(Math.random() / 6 + 1); if (childName === "leaf") { wind.push(Math.random() / 4 + 0.5); newChild.className = childName + Math.floor(Math.random() * 2); let random = Math.random() * 15; newChild.style.width = (60 - random) + "px"; newChild.style.height = (60 - random) + "px"; } else { wind.push(0.2); newChild.className = childName + Math.floor(Math.random()); let random = Math.random() * 5; newChild.style.width = (10 - random) + "px"; newChild.style.height = (10 - random) + "px"; } rot.push(Math.random() / 4 + 0.4); delta.push(Math.random() * 40 - 20); if (z < 90) { dir.push(-1); } else { dir.push(1); } document.getElementsByClassName("container")[0].appendChild(newChild); $(newChild).fadeIn("slow"); } function randomRotate() { let x = Math.random() * 10; let y = Math.random() * 30 - 15; let z = Math.random() * 180; return "rotateX(" + x + "deg) rotateY(" + y + "deg) rotateZ(" + z + "deg)"; } //Animates the children of a given parent to fall function animate(parent) { if (curLeaves < numLeaves) { let bound = (numLeaves - curLeaves); for (let i = 0; i < bound; i++) { setTimeout(function () { createChild(document.getElementsByClassName("tree")[0], "leaf"); }, (wait * i)); curLeaves++; } } if (curSnow < numSnow) { let bound = (numSnow - curSnow); for (let i = 0; i < bound; i++) { setTimeout(function () { createChild(document.getElementsByClassName("sky")[0], "snowflake"); }, ((wait + 100) * i)); curSnow++; } } for (let i = 0; i < parent.children.length; i++) { let child = parent.children[i]; let z = child.style.transform.split(" ")[2].replace("rotateZ(", "").replace("deg)", ""); let dx = speed[i]; let dy = Math.random() * 2 * Math.abs(Math.cos(z * Math.PI / 180)); if (child.className.indexOf("leaf") >= 0) { child.style.top = (child.style.top.replace("px", "") - (0.2 * Math.sin(z / 180 * Math.PI)) + grav[i] + "px"); } else { child.style.top = (child.style.top.replace("px", "") - 0 + grav[i] + "px"); } child.style.left = (child.style.left.replace("px", "") - 0 - (0.1 * Math.sin(z / 180 * Math.PI)) + (dir[i] * speed[i] * 1.5) + wind[i]) + "px"; if ((child.style.top.replace("px", "") - 0 + dy) > height || (child.style.left.replace("px", "") - 0 + dx) > width || (child.style.left.replace("px", "") - 0 + dx) < -100) { parent.removeChild(child); dir.splice(i, 1); speed.splice(i, 1); wind.splice(i, 1); grav.splice(i, 1); rot.splice(i, 1); delta.splice(i, 1); if (child.className.indexOf("leaf") >= 0) { curLeaves--; } else { curSnow--; } i--; } else { if (z >= 92 && dir[i] === -1) { if (speed[i] > 0) { speed[i] -= speed[i] / 50; } } else if (z >= 100 && dir[i] === 1) { if (speed[i] < 1.1) { speed[i] += 0.02; } } else if (z <= 80 && dir[i] === -1) { if (speed[i] < 1.1) { speed[i] += 0.03; } } else if (z <= 88 && dir[i] === 1) { if (speed[i] > 0) { speed[i] -= speed[i] / 50; } } if (z <= 92 && z >= 88) { speed[i] = 1 + (Math.random() / 4); } if ((z <= 140 && dir[i] === -1 && speed[i] > 0.22) || (z >= 20 && dir[i] === 1 && speed[i] > 0.22)) { z = z - (dir[i] * rot[i]); } if (z >= 110 && speed[i] <= 0.2 && dir[i] === -1) { dir[i] = 1; } else if (z < 70 && speed[i] <= 0.2 && dir[i] === 1) { dir[i] = -1; } let x = child.style.transform.split(" ")[0].replace("rotateX(", "").replace("deg)", ""); if (x > 0 + delta[i] && dir[i] === -1) { x = x - Math.random() / 2; } else if (x < 40 + delta[i] && dir[i] === 1) { x = x - 0 + Math.random() / 2; } let y = child.style.transform.split(" ")[1].replace("rotateY(", "").replace("deg)", ""); if (y > 0 + delta[i] && dir[i] === -1) { y = y - Math.random() / 2; } else if (y < 40 + delta[i] && dir[i] === 1) { y = y - 0 + Math.random() / 2; } child.style.transform = "rotateX(" + x + "deg) rotateY(" + y + "deg) rotateZ(" + z + "deg)"; } } } setInterval(function () { animate(document.getElementsByClassName("container")[0]); }, 10); /* END OF PROCEDURE */ }, false); </script> |
Осталось лишь залить все картинки из прикреплённого архива в папку img