Skip to content
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

Possible to stop animated gifs when navigate away from section? #592

Closed
eabigelow opened this issue Sep 7, 2013 · 14 comments
Closed

Possible to stop animated gifs when navigate away from section? #592

eabigelow opened this issue Sep 7, 2013 · 14 comments

Comments

@eabigelow
Copy link

Hi--

I love working with reveal.js--very easy and flexible to use. Thanks so much for that!

I was wondering if it is possible to have animated gifs only play when the user navigates to the particular section that the animated gif is on? Currently, you can do this with videos (works great), but it would also be very useful if animated gifs only ran when the user was on that section were the gif is. It would make the slider work more smoothly because the gifs would not all be playing in the background.

Any thoughts about this? Or fixes? Again, thanks for the great work!

@rparree
Copy link
Contributor

rparree commented Sep 8, 2013

You could replace the src of the animated gif with a static image, and then when the section becomes active replace it with the actual gif URL:

$(function () {
    // on page load  replace all src URLs

    $('.animated-gif').each(function (i, e) {
        disableGif(e)
    });
});
Reveal.addEventListener('slidechanged', initAnimatedGifs);
Reveal.addEventListener('ready', initAnimatedGifs);

function initAnimatedGifs(event) {
    $('.animated-gif', event.currentSlide).each(function (i, e) {
        enableGif(e)
    });

    if (event.previousSlide)
        $('.animated-gif', event.previousSlide).each(function (i, e) {
            disableGif(e)

        });
}

function enableGif(e) {
    var src = $(e).attr("src")
    $(e).attr('src', src.replace('-image.gif', '.gif'));


}
function disableGif(e) {
    var src = $(e).attr("src")
    $(e).attr('src', src.replace('.gif', '-image.gif'));

}

@eabigelow
Copy link
Author

Hi, rparree--

Thank you very much for this.

Could you tell me exactly how to implement this, please? I am not sure how the modified html should look, and as a terribly poor newbie to js, I am also not exactly sure how to modify your .js script for my slider.

Here is an example of the current code for one section:

[CODE]

   <div class="animations"><img alt="image here" width="100%" height="100%" src="animations/1.gif"> </div>   

[/CODE]

animations/1.gif is the animation which I would like to play from the beginning (it only plays once). Also, I have called the .js script with this in the header: <script type="text/javascript" src="js/gifs.js"></script> (I named your js script as "gifs").

Again, many thanks for any help!

@eabigelow
Copy link
Author

Oops! Sorry, I forgot to mention that I will also be doing this about seven times in different places throughout the slider.

Again, thanks!

@rparree
Copy link
Contributor

rparree commented Sep 8, 2013

Just place the snippet in the last script element in your html page after the Reveal.initialize
and add the following class to your img: animated-gif

@eabigelow
Copy link
Author

Wow. Works like a charm! Thanks so much, rparree! If it's ok, can I pay pal you $20 for your trouble?

@rparree
Copy link
Contributor

rparree commented Sep 8, 2013

no worries...just helping out :) glad i could help

@eabigelow
Copy link
Author

Thanks very much again! Quick question, though. When I apply the class to the animation, it kicks it halfway down the page. It still works, but it won't place in the center of the page. Is there a way to correct that?

@eabigelow
Copy link
Author

I see when I go into reveal.min.css and change .reveal .slides{..... top:50% to top: 0% that solves the problem, but it also kicks everything else to the top of the page....

@eabigelow
Copy link
Author

No problems in Chrome, by the way. Firefox is where the trouble lies...

@rparree
Copy link
Contributor

rparree commented Sep 9, 2013

I am not sure if it is related. However do note that you are setting the CSS class, so you might want to ensure you apply the same style as img:

.animated-gif{
    max-width: 95%;
    max-height: 95%;
}

Try setting the width and height attributes on your img as well (to the actual size of your gif)

Note: don't edit the reveal.min.css, this file is generated using grunt (cssmin) from reveal.css. Even better use your own css file, so you can easily pull updates from git without having to merge the css files.

@eabigelow
Copy link
Author

Hi, rparree--

Thanks for the advice. I think I found a solution by adding the style to the end of the javascript, like this:

function disableGif(e) {
'.animated-gif'.style.top="0%";

var src = $(e).attr("src")
$(e).attr('src', src.replace('.gif', '-image.gif'));

}
</script>

This seems to have worked (I hope). Again, thanks for all your help! Much appreciated!

PS Are you ever available for small coding jobs here and there?

@hakimel
Copy link
Owner

hakimel commented Sep 9, 2013

Glad you guys were able to work this out, thanks @rparree

@hakimel hakimel closed this as completed Sep 9, 2013
@rparree
Copy link
Contributor

rparree commented Sep 11, 2013

@eabigelow ref "available for small coding jobs". Yes i am available for small coding jobs (as long as they are small enough not to interfere with my already busy schedule :). Please contact me by email (see my profile page)

@ynuska
Copy link

ynuska commented Feb 27, 2018

How can I pause and resume the animated gif ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants