JSFiddle - React, Tailwind, and code Playground
by John Pepp
HTML
<div class="main">
<div class="box1"></div>
<div class="box2"></div>
<button class="myButton1">Button1</button>
<button class="myButton2">Button2</button>
</div>
CSS
body {
font-size: 100%;
}
.box1 {
display: block;
position: relative;
left: 0;
background-color: orange;
width: 50px;
height: 50px;
}
.box2 {
display: block;
position: relative;
top: 25px;
left: 0;
background-color: magenta;
width: 50px;
height: 50px;
}
.box2 {
background-color: magenta;
}
.myButton1 {
display: block;
outline: none;
position: absolute;
top: 240px;
left: 270px;
width: 120px;
height: 50px;
cursor: pointer;
border: none;
border-radius: 25px;
background-color: orange;
color: white;
z-index: 999;
}
.myButton2 {
display: block;
outline: none;
position: absolute;
top: 240px;
left: 400px;
width: 120px;
height: 50px;
cursor: pointer;
border: none;
border-radius: 25px;
background-color: magenta;
color: white;
z-index: 999;
}
/* Styling for the OFF Position */
.off {
background-color: rgba(153, 0, 0, 0.7);
}
JavaScript
$(function () {
var myButton1 = $('.myButton1'), // The Button:
box1 = $('.box1'),
myButton2 = $('.myButton2'),
box2 = $('.box2');
myButton1.on('click', function () {
$(this).toggleClass('off');
if ($(this).hasClass('off')) {
moveRight(); // Animate Box to the Right:
} else {
moveLeft(); // Move Box back to the Original Position:
}
});
myButton2.hover(function () {
box2.animate({
'left': '300px'
});
$(this).css('background-color', 'orange');
}, function () {
box2.animate({
'left': 0
});
});
function moveLeft() {
box1.animate({
'left': 0
});
}
function moveRight() {
box1.animate({
'left': '300px'
});
}
}); // End of Document Ready: