Skip to content

"remove" method on instance #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3798,7 +3798,10 @@ <h3 class="demo-title">remove</h3>
Removes targets from a running animation or timeline.<br>
The <code>targets</code> parameters accepts the same values as the <a class="color-targets" href="#cssSelector">targets</a> property.
</p>
<p>Removes targets from all active animations.</p>
<pre><code>anime.remove(targets)</code></pre>
<p>Removes targets from the a single animation or timeline.</p>
<pre><code>animation.remove(targets)</code></pre>
</div>
<div class="demo-content remove-demo">
<div class="line">
Expand All @@ -3819,7 +3822,7 @@ <h3 class="demo-title">remove</h3>
</div>
<script>var remove = function() {
/*DEMO*/
anime({
var animation = anime({
targets: '.remove-demo .el',
translateX: 270,
direction: 'alternate',
Expand All @@ -3828,7 +3831,7 @@ <h3 class="demo-title">remove</h3>
});

document.querySelector('.remove-el-button').addEventListener('click', function() {
anime.remove('.remove-demo .line:nth-child(2) .el');
animation.remove('.remove-demo .line:nth-child(2) .el');
});
/*DEMO*/
}
Expand Down
28 changes: 16 additions & 12 deletions lib/anime.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1178,20 +1178,24 @@ function removeTargetsFromAnimations(targetsArray, animations) {
}
}

function removeTargets(targets) {
function removeTargetsFromInstance(targetsArray, instance) {
var animations = instance.animations;
var children = instance.children;
removeTargetsFromAnimations(targetsArray, animations);
for (var c = children.length; c--;) {
var child = children[c];
var childAnimations = child.animations;
removeTargetsFromAnimations(targetsArray, childAnimations);
if (!childAnimations.length && !child.children.length) { children.splice(c, 1); }
}
if (!animations.length && !children.length) { instance.pause(); }
}

function removeTargetsFromActiveInstances(targets) {
var targetsArray = parseTargets(targets);
for (var i = activeInstances.length; i--;) {
var instance = activeInstances[i];
var animations = instance.animations;
var children = instance.children;
removeTargetsFromAnimations(targetsArray, animations);
for (var c = children.length; c--;) {
var child = children[c];
var childAnimations = child.animations;
removeTargetsFromAnimations(targetsArray, childAnimations);
if (!childAnimations.length && !child.children.length) { children.splice(c, 1); }
}
if (!animations.length && !children.length) { instance.pause(); }
removeTargetsFromInstance(targetsArray, instance);
}
}

Expand Down Expand Up @@ -1286,7 +1290,7 @@ anime.speed = 1;
// TODO:#review: naming, documentation
anime.suspendWhenDocumentHidden = true;
anime.running = activeInstances;
anime.remove = removeTargets;
anime.remove = removeTargetsFromActiveInstances;
anime.get = getOriginalTargetValue;
anime.set = setTargetsValue;
anime.convertPx = convertPxToUnit;
Expand Down
Loading