Clicking a Button fast buggs JavaScript - javascript

var Buttons = new Array();
var AnimEnd = new Array();
var Called = new Array();
function Start () {
Buttons = document.getElementsByClassName("google_button-main");
for (var i = 0; i < Buttons.length; i++)
{
Buttons[i].onmousedown = (e) => {
var Animator = e.target.previousElementSibling;
Called[i] = false;
AnimEnd[i] = false;
Animator.setAttribute("Animated", "true");
Animator.ontransitionend = (f) => {
if (Called[i] == false)
{
if (AnimEnd[i] == true)
{
Animator.setAttribute("Animated", "false");
}
}
Called[i] = true;
AnimEnd[i] = true;
}
}
Buttons[i].onmouseup = (e) => {
var Animator = e.target.previousElementSibling;
if (AnimEnd[i] == true)
{
Animator.setAttribute("Animated", "false");
}
AnimEnd[i] = true;
}
}
}
.google_button-main {
height: 100%;
width: 100%;
border: none;
background-color: black;
z-index: -1;
pointer-events: auto;
}
.google_button-animator[animated="false"] {
height: 0px;
width: 0px;
border-radius:50%;
background-color: rgba(255, 255, 255, 0.0);
transition: background-color 0.4s ease, height 0.4s step-end, width 0.4s step-end, border-radius 0.4s step-end;
}
.google_button-animator {
position: absolute;
pointer-events: none;
z-index: 1;
}
.google_button-animator[animated="true"] {
height: 50px;
width: 50px;
background-color: rgba(255, 255, 255, 0.5);
border-radius: 0%;
transition: height 0.4s ease, width 0.4s ease, border-radius 0.4s ease;
}
.google_button-wrapper {
position: relative;
pointer-events: none;
height: 50px;
width: 50px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body onload="Start();">
<div class="google_button-wrapper">
<div class="google_button-animator" animated="false">
</div>
<button class="google_button-main">
Knopf
</button>
</div>
</body>
</html>
I wanted to make a button-animation like the Google buttons.
I wanted the animation, to be played on button-down and on button-up, but only when the previous animation is finished. So I added the "AnimEnd"-array.
After that I ran into the issue, that the ontransitionend-function gets called 5 times after the transition is done, so I added the "Called"-array.
My code works, but when I click the button (In the snippet below) very fast the JavaScript-onmouseup stops working.
I tried to resolve it with the console-log, but I couldnt find the issue!

Related

How do I change background images like on the Flickr homepage?

I'm trying to clone the Flickr homepage and I want the first image to drop in in .3s and then for the images to change every 4 seconds taking 1 second to transition unless ArrowLeft or ArrowRight is pressed, in the case of ArrowLeft the image should have a drop in animation to the previous image in the cycle and in the case of ArrowRight being pressed the image should drop in to the next image in the cycle and in the case of both the images should stay for 4 seconds NOT TRANSITIONING IN LESS TIME IF THE INTERVAL THAT STARTS ON PAGE LOAD IS INTERRUPTED. You can see what I mean on the Flickr homepage.
the following is a distillation of the problem in code.
html document
<!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>test</title>
<link rel="stylesheet" href="test.css">
<script src="test.js" defer></script>
</head>
<body>
<div id="background"></div>
<div id='grid'>
<header>header</header>
<main>main</main>
<footer>footer</footer>
</div>
</body>
</html>
css document
* {
margin: 0;
padding: 0;
}
body {
background-color: rgb(50, 50, 50);
display: grid;
}
#background {
height: 100vh;
width: 100%;
background-image: url(./test_images/meditation.jpg);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
animation-duration: .3s;
animation-timing-function: ease-out;
animation-delay: 0s;
animation-iteration-count: 1;
animation-name: slideIn;
transition: background-image 1s;
z-index: 0;
}
#keyframes slideIn {
0% {
transform: translateY(-100%);
}
100% {
transform: translateY(0%);
}
}
#grid {
display: grid;
grid-template-rows: 65px 1fr minmax(65px, auto);
height: 100vh;
width: 100%;
z-index: 1;
}
header {
background-color: rgba(0, 0, 0, .5);
color: white;
}
main {
color: white;
}
footer {
color: white;
background-color: black;
}
#grid, #background {
grid-area: 1 / 1;
}
javascript document
let i = 0;
const backgroundImages = ['meditation.jpg', 'fish.jpg', 'fern.jpg', 'stars.jpg', 'northernLights.jpg', 'forest.jpg', 'mountains.jpg', 'horse.jpg', 'lion.jpg', 'engineer.jpg', 'computers.jpg'];
function changeImages () {
i++;
if (i == backgroundImages.length) {i = 0}
document.getElementById('background').style.backgroundImage = 'url(./test_images/' + backgroundImages[i] + ')';
}
window.onload = function () {
window.setInterval(changeImages, 4000);
}
document.addEventListener('keydown', (event) => {
if (event.key == "ArrowLeft") {
i--;
} else if (event.key == "ArrowRight") {
i++;
}
if (i == -1) {
i = backgroundImages.length - 1;
} else if (i >= backgroundImages.length) {
i = 0;
}
document.getElementById('background').style.backgroundImage = 'url(./test_images/' + backgroundImages[i] + ')';
document.getElementById('background').animate([
{transform: "translateY(-100%)"},
{transform: 'translateY(0%)'}
], {
duration: 300,
iterations: 1
})
window.setInterval(changeImages, 4000);
}, false)
Each time you set a new interval, cancel the old interval using clearInterval:
let intervalId;
window.onload = function () {
intervalId = window.setInterval(changeImages, 4000);
}
document.addEventListener('keydown', (event) => {
.....
clearInterval(intervalId);
intervalId = window.setInterval(changeImages, 4000);
}, false)

JS card animation + scss

My idea is to create a smooth animation of the appearance of the card from anywhere on the screen to the center. I already have an attractive animation with simultaneous zoom and rotation. It remains to make sure that on click the card from any center of the screen moves smoothly to the center, when closed, to its original position. It is advisable to do this in pure js. It is possible with the connection of libraries, it will also be useful
JS:
card = document.querySelector(".card");
front = document.querySelector(".front");
back = document.querySelector(".back");
exit = document.querySelector(".back-exit");
// //data of screen
// pageWidth = document.documentElement.scrollWidth;
// pageHeight = document.documentElement.scrollHeight;
// //date for front part
// heightOfBlock = front.offsetHeight;
// widthOfBlock = front.clientWidth;
// //date for back part
// heightOfBlock2 = back.offsetHeight;
// widthOfBlock2 = back.clientWidth;
front.addEventListener("click", function () {
front.classList.add("active");
back.classList.add("active");
back.classList.add("scale");
function background() {
card.classList.add("active");
}
// front.style.top = (pageHeight / 2) - (heightOfBlock / 2) + 'px';
// front.style.left = (pageWidth / 2) - (widthOfBlock / 2) + 'px';
// back.style.top = (pageHeight / 2) - 300 + 'px';
// back.style.left = (pageWidth / 2) - 500 + 'px';
setTimeout(background, 500);
});
exit.addEventListener("click", function () {
front.classList.remove("active");
back.classList.remove("active");
back.classList.remove("scale");
card.classList.remove("active");
// front.style.top = 0 + 'px';
// front.style.left = 0 + 'px';
// back.style.top = 0 + 'px';
// back.style.left = 0 + 'px';
});
* {
margin: 0;
padding: 0;
font-family: Roboto, sans-serif;
}
body {
overflow: hidden;
}
.card {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.card.active {
background-color: rgba(0, 0, 0, 0.8);
transition: background 1s;
}
.front,
.back {
cursor: pointer;
width: 298px;
height: 199px;
overflow: hidden;
backface-visibility: hidden;
position: absolute;
transition: transform 0.5s linear;
border-radius: 30px;
background-color: #111;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.front {
transform: perspective(600px) rotateY(0deg);
transition: 0s ease;
}
.back {
transform: perspective(600px) rotateY(540deg);
background-color: rgb(70, 70, 70);
transition: 0s ease;
&-exit {
position: absolute;
top: 20px;
right: 20px;
color: rgb(126, 19, 19);
&:hover {
text-decoration: underline;
}
}
}
.front.active {
transform: perspective(600px) rotateY(-540deg);
transition: 1.2s ease;
animation: 1s linear scale;
}
.back.active {
transform: perspective(600px) rotateY(0deg);
transition: 1.2s ease;
font-size: 3rem;
.back-exit {
font-size: 1.3rem;
}
/* Запускать анимацию, когда класс активный */
animation: 1s linear scale;
}
.back.scale {
width: 1000px;
height: 600px;
}
/* Написать анимацию */
#keyframes scale {
from {
width: 298px;
height: 199px;
}
to {
width: 1000px;
height: 600px;
}
}
<!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">
<link rel="stylesheet" href="./style.css">
<title>Document</title>
</head>
<body>
<div class="card">
<div class="front">
Hello It's me!
</div>
<div class="back">
<div class="back-exit">close</div>
Bye!
</div>
</div>
<!-- <h1>По клику блок выровняется.</h1>
<h2>Можно проскролить и кликнуть где-нибудь внизу.</h2>
<div id="testBlock"></div> -->
<script src="./script.js"></script>
</body>
</html>
The code is a little incorrect because of what I'm doing in sass

How to change the background image of an element randomly?

I have recently created a program which creates new element when someone clicks it. The new element which is created has some specific CSS styling. Now i want it's background to randomly change when the user clicks on button. Like the first time when someone clicks the background is red another time its green and so on like this.. My code is -
function a1click(){
var body = document.querySelector('body');
var bubbles = document.createElement("span");
var size = Math.random() * 100;
bubbles.style.width = 10 + size+'px';
bubbles.style.height = 10 + size+'px';
body.appendChild(bubbles);
setTimeout(function(){
bubbles.remove();
},1000)
}
*{
margin: 0;
padding: 0;
}
body{
background: rgb(73, 156, 145);
}
#a1{
position: relative;
top: 350px;
left: 100px;
width: 30px;
height: 150px;
border: 2px solid #fff;
background: rgb(0, 0, 0);
float: left;
cursor: pointer;
perspective: 600;
}
span{
position: absolute;
top: 120px;
left: 60%;
width: 50px;
height: 50px;
background: #000;
animation: tweek 1s linear infinite;
transform-origin: top;
background-size: cover;
pointer-events: none;
}
#keyframes tweek {
0% {
transform: rotate(90deg) translate(300px);
}
100% {
transform: rotate(0deg) translate(250px);
opacity: 0;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body onkeydown="keypress(event)">
<div id="a1" onclick="a1click()"></div>
<script src="script.js"></script>
</body>
</html>
I want the background color of this box to change randomly..Please help, any help is appreciated..
as you can see from the code, I created a function that randomizes the numbers and puts them in the rgb.
function random_bg_color() {
var x = Math.floor(Math.random() * 256);
var y = Math.floor(Math.random() * 256);
var z = Math.floor(Math.random() * 256);
var bgColor = "rgb(" + x + "," + y + "," + z + ")";
return bgColor;
}
function a1click(){
var body = document.querySelector('body');
var bubbles = document.createElement("span");
var size = Math.random() * 100;
bubbles.style.width = 10 + size+'px';
bubbles.style.height = 10 + size+'px';
bubbles.style.backgroundColor = random_bg_color();
body.appendChild(bubbles);
setTimeout(function(){
bubbles.remove();
},1000)
}
*{
margin: 0;
padding: 0;
}
body{
background: rgb(73, 156, 145);
}
#a1{
position: relative;
top: 350px;
left: 100px;
width: 30px;
height: 150px;
border: 2px solid #fff;
background: rgb(0, 0, 0);
float: left;
cursor: pointer;
perspective: 600;
}
span{
position: absolute;
top: 120px;
left: 60%;
width: 50px;
height: 50px;
background: #000;
animation: tweek 1s linear infinite;
transform-origin: top;
background-size: cover;
pointer-events: none;
}
#keyframes tweek {
0% {
transform: rotate(90deg) translate(300px);
}
100% {
transform: rotate(0deg) translate(250px);
opacity: 0;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body onkeydown="keypress(event)">
<div id="a1" onclick="a1click()"></div>
<script src="script.js"></script>
</body>
</html>
For answer your comment :
Generate random image:
var Images = new Array("https://via.placeholder.com/150/0000FF/808080","https://via.placeholder.com/150/FF0000/FFFFFF",
"https://via.placeholder.com/150/FFFF00/000000");
function randompic() {
var randomNum = Math.floor(Math.random() * Images.length);
return Images[randomNum];
}
function a1click(){
var body = document.querySelector('body');
var bubbles = document.createElement("span");
var size = Math.random() * 100;
bubbles.style.width = 10 + size+'px';
bubbles.style.height = 10 + size+'px';
bubbles.style.backgroundImage = "url('"+randompic()+"')";
body.appendChild(bubbles);
setTimeout(function(){
bubbles.remove();
},1000)
}
*{
margin: 0;
padding: 0;
}
body{
background: rgb(73, 156, 145);
}
#a1{
position: relative;
top: 350px;
left: 100px;
width: 30px;
height: 150px;
border: 2px solid #fff;
background: rgb(0, 0, 0);
float: left;
cursor: pointer;
perspective: 600;
}
span{
position: absolute;
top: 120px;
left: 60%;
width: 50px;
height: 50px;
background: #000;
animation: tweek 1s linear infinite;
transform-origin: top;
background-size: cover;
pointer-events: none;
}
#keyframes tweek {
0% {
transform: rotate(90deg) translate(300px);
}
100% {
transform: rotate(0deg) translate(250px);
opacity: 0;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body onkeydown="keypress(event)">
<div id="a1" onclick="a1click()"></div>
<script src="script.js"></script>
</body>
</html>
This seems to work:
http://jsfiddle.net/mcuzq9rb/
I included a random color generator and applied it to the style of the floating squares.
Let me know if it's what you wanted.
I would style the box with an rga-value, similar to how you randomly set the size of the bubble:
var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
bubbles.style.backgroundColor = "rgb("+r+", "+g+", "+b+")";
Here's a little function that uses rando.js to set an element's background color to a random hex string.
function setRandomBackgroundColor(element) {
var hexString = "#";
for (var i = 0; i < 3; i++) {
var hex = rando(255).toString(16);
hexString += hex.length == 1 ? "0" + hex : hex;
}
element.style.backgroundColor = hexString;
}
<script src="https://randojs.com/1.0.0.js"></script>
<button onclick="setRandomBackgroundColor(document.body);">Set random background color</button>

Stop animation after 3 loops

I'm trying to get a looping slideshow to stop looping after 3 times and have it end on the last frame. It's a 300x250 web banner with 3 different frames.
Any help would be appreciated, thank you!
<style>
#frames {
position: absolute;
overflow: hidden;
top: 0;
left: 0;
width: 300px;
height: 250px;
}
#frames a {
position: absolute;
}
#frames a:nth-of-type(1) {
animation-name: fader;
animation-delay: 3s;
animation-duration: 1s;
z-index: 20;
}
#frames a:nth-of-type(2) {
z-index: 10;
}
#frames a:nth-of-type(n+3) {
display: none;
}
#keyframes fader {
from { opacity: 1.0; }
to { opacity: 0.0; }
}
</style>
<div id="frames">
<img src="01.jpg">
<img src="02.jpg">
<img src="03.jpg">
</div>
<script>
window.addEventListener("DOMContentLoaded", function(e) {
var frames = document.getElementById("frames-1");
var fadeComplete = function(e) { frames.appendChild(arr[0]); };
var arr = frames.getElementsByTagName("a");
for(var i=0; i < arr.length; i++) {
arr[i].addEventListener("animationend", fadeComplete, false);
}
}, false);
</script>
Hey I made an example how you could animate 3 iterations. Note that I wrote some dummy code that is not refactored or a fully implemented slider at all. But it shows the principle.
What happens is that if you click the button it will 'plan' 3 animation cycles at a 5000ms interval.
Alternatively you could recurse the animations instead of planning them to make the code a bit more flexible.
var currentlySelectedNode = 0;
var nodes = document.querySelectorAll('#container>div');
var nextButton = document.getElementById('next');
function showNode( node ) {
node.classList.add('show');
}
function hideNode( node ) {
node.classList.remove('show');
}
function showNextNode() {
hideNode( nodes[currentlySelectedNode] );
if( currentlySelectedNode < nodes.length - 1 )
currentlySelectedNode++;
else
currentlySelectedNode = 0;
showNode( nodes[currentlySelectedNode] );
}
function showNext3Nodes() {
showNextNode();
window.setTimeout( ()=>{
showNextNode();
}, 5000);
window.setTimeout( ()=>{
showNextNode();
}, 10000);
}
nextButton.addEventListener('click', function() {
showNext3Nodes();
});
showNode(nodes[0]);
#container{
position: relative;
height: 100px;
width: 100px;
}
#container>div {
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
opacity: 0;
transition: opacity 2s;
}
#container>div.show{
opacity: 1;
}
<div id='container'>
<div class='show' style='background: red'></div>
<div style='background: blue'></div>
<div style='background: green'></div>
<div style='background: yellow'></div>
<div style='background: purple'></div>
<div style='background: orange'></div>
</div>
<button id='next'>Next(3)</button>
Note: You can do the same with css by setting various animation delays to different elements. This would however be quite inflexible.
please add Jquery to your code and use my snippet code.
setTimeout(function(){
$('#frames a').addClass('stop_animation');
},17000);
$('#frames a').click(function(){
$('#frames a').addClass('stop_animation');
});
#frames{
width: 300px;
height: 250px;
overflow:hidden;
position:relative;
}
#frames a{
position:absolute;
animation: fader 6s 3;
-webkit-animation: fader 6s 3;
opacity:0;
width:100%;
height: 100%;
}
#frames a:nth-child(1){-webkit-animation-delay:0s;}
#frames a:nth-child(2){-webkit-animation-delay:2s;}
#frames a:nth-child(3){-webkit-animation-delay:4s;}
#-webkit-keyframes fader{
25%{opacity:1;}
40%{opacity:0;}
}
#frames a.stop_animation{
animation-play-state: paused;
opacity: 1;
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Teste</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div id="frames">
<img src="https://upload.wikimedia.org/wikipedia/commons/2/24/Ad-MediumRectangle-300x250.jpg">
<img src="http://transativafm.com.br/wp-content/uploads/2018/03/anuncie-300x250.png">
<img src="https://www.portalmongagua.com.br/images/anuncios/54dc18565648636b421710d98b2def02.png">
</div>
</body>
</html>

Text transition/animation onclick in javascript function and HTML

I have a button triggering this function on click:
<button onclick="showAdvice()">Advice!</button>
<p id="text"></p>
<script>
function showAdvice() {
var advices = ["1","2","3"];
var choose = Math.floor(Math.random() * advices.length);
document.getElementById("text").innerHTML = advices[choose];
}
</script>
The onclick action generates advices in "text" paragraph, they appear in random order. I want them to have a transition in between - opacity, fade out/fade in. How can I achieve this in CSS/JavaScript?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<style type="text/css" media="screen">
#pContainer {
position: relative;
overflow: hidden;
width: 200px;
height: 200px;
}
#pContainer > p {
position: absolute;
top: 0;
left: 0;
transition: opacity 0.5s linear;
}
#pContainer > p.show {
opacity: 1;
}
#pContainer > p.hide {
opacity: 0;
}
</style>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<button onclick="showAdvice()">Advice!</button>
<div id="pContainer">
<p id="text" class="show"></p>
<p id="text02" class="hide"></p>
</div>
<script>
function showAdvice() {
var advices = ["1","2","3"];
var choose = Math.floor(Math.random() * advices.length);
$('#pContainer p.hide')
.text(advices[choose])
.removeClass('hide')
.addClass('show')
.siblings('p')
.removeClass('show')
.addClass('hide');
}
</script>
</body>
</html>
You can use jquery to create animations.
function showAdvice() {
$("#text").hide();
var advices = ["1", "2", "3"];
var choose = Math.floor(Math.random() * advices.length);
document.getElementById("text").innerHTML = advices[choose];
$("#text").fadeIn();
}
Example:
https://jsfiddle.net/90mxq8bf/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<style type="text/css" media="screen">
#pContainer {
position: relative;
overflow: hidden;
width: 200px;
height: 100px;
}
#pContainer > p {
position: absolute;
top: 0;
left: 0;
/* transition: opacity 0.5s linear; */
}
#pContainer > p.show {
/* opacity: 1; */
opacity: 1;
-webkit-animation: showP 0.85s linear;
}
#pContainer > p.hide {
/* opacity: 0; */
opacity: 0;
top: -50px;
-webkit-animation: hideP 0.85s linear;
}
#-webkit-keyframes showP {
0%{
opacity: 0;
top: -50px;
}
100%{
opacity: 1;
top: 0px
}
}
#-webkit-keyframes hideP {
0%{
opacity: 1;
top: 0px;
}
80%{
opacity: 0;
}
90%{
top: 105px; /* container height + 5*/
}
100%{
opacity: 0;
top: -50px;
}
}
</style>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<button onclick="showAdvice()">Advice!</button>
<div id="pContainer">
<p id="text" class="show"></p>
<p id="text02" class="hide"></p>
</div>
<script>
function showAdvice() {
var advices = ["1","2","3"];
var choose = Math.floor(Math.random() * advices.length);
$('#pContainer p.hide')
.text(advices[choose])
.removeClass('hide')
.addClass('show')
.siblings('p')
.removeClass('show')
.addClass('hide');
}
</script>
</body>
</html>

Categories