Skip to content

Instantly share code, notes, and snippets.

@K4mey
Created April 12, 2021 15:40
Show Gist options
  • Save K4mey/75636dc41927b9ca2bf56b21446a9769 to your computer and use it in GitHub Desktop.
Save K4mey/75636dc41927b9ca2bf56b21446a9769 to your computer and use it in GitHub Desktop.
Pausing Transitions
<h3>Pure Javascript</h3>
<div class='box'></div>
<button class='toggleButton' value='play'>Play</button>
<h3>jQuery</h3>
<div class='box'></div>
<button class='toggleButton' value='play'>Play</button>

Pausing Transitions

Pausing CSS transitions using a button with pure javascript and jQuery for a CSS Trick's article

A Pen by Zach Saucier on CodePen.

License.

var boxOne = document.getElementsByClassName('box')[0],
$boxTwo = $('.box:eq(1)');
document.getElementsByClassName('toggleButton')[0].onclick = function() {
if(this.innerHTML === 'Play')
{
this.innerHTML = 'Pause';
boxOne.classList.add('horizTranslate');
} else {
this.innerHTML = 'Play';
var computedStyle = window.getComputedStyle(boxOne),
marginLeft = computedStyle.getPropertyValue('margin-left');
boxOne.style.marginLeft = marginLeft;
boxOne.classList.remove('horizTranslate');
}
}
$('.toggleButton:eq(1)').on('click', function() {
if($(this).html() === 'Play')
{
$(this).html('Pause');
$boxTwo.addClass('horizTranslate');
} else {
$(this).html('Play');
var computedStyle = $boxTwo.css('margin-left');
$boxTwo.removeClass('horizTranslate');
$boxTwo.css('margin-left', computedStyle);
}
});
<script src="https://code.jquery.com/jquery-2.0.0.js"></script>
.box {
margin: 30px;
height: 50px;
width: 50px;
background-color: blue;
}
.box.horizTranslate {
-webkit-transition: 3s;
-moz-transition: 3s;
-ms-transition: 3s;
-o-transition: 3s;
transition: 3s;
margin-left: 50% !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment