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

Programmatic way to go to a slide? #26

Closed
HongPong opened this issue Jul 14, 2016 · 7 comments
Closed

Programmatic way to go to a slide? #26

HongPong opened this issue Jul 14, 2016 · 7 comments

Comments

@HongPong
Copy link

Hi,

Thanks for all your help before, the snapping is working very well. I am wondering if there is a way to go programmatically to a slide, if I call a JS event (preferable) or call going to an anchor (also cool). I.e. to make a table of contents that can be clicked to bring you to a slide. Thank you!

@HongPong
Copy link
Author

I am doing a workaround similar to this:

        window.getPanelHeight = function() {
            var firstpanel = $('.dm-pnl:first');
            window.panelheight = parseInt($(firstpanel).css('min-height').replace('px', ''));
            console.log(window.panelheight);
        };

        // debounced resize event catcher.
        $( window ).resize(function() {
            window.getPanelHeight();
            var paneltimeout = window.setTimeout(getPanelHeight, 1000);
        });

        window.goToSlide = function(num) {
            var oneheight = window.panelheight;
            var targetheight = oneheight * num;
            $("html,body").scrollTop(targetheight);
        }

@jh3y
Copy link
Owner

jh3y commented Jul 21, 2016

Hey @HongPong

Sorry for the delayed response.

To programatically navigate to a slide will require a workaround. Doing a simple window.scrollTo would suffice but then you lose the animation and can hit z-index issues.

To retain the animation, the quickest solution will be to use jQuery. Also, each panel element has a STARTING_POS property which tells us where to scroll to in the body to get to the start of a panel.

This will of course first require that you have jQuery available to you on the page. Then add the following;

window.Doormat.prototype.travelTo = function(index) {
  var panels = document.querySelectorAll('.dm-pnl');
  if (panels[index - 1]) {
    var pos = panels[index - 1].STARTING_POS;
    if (pos !== undefined) $('body').animate({scrollTop: pos});
  } else {
    throw Error('Doormat: no panel available at that index!');
  }
}

The result will be that you can slide to any panel you want by index. For example;

var myDoormat = new Doormat();
myDoormat.travelTo(4);

Hope that helps you out! 😎

I'll add a note about this in the README.

@jh3y

@jh3y jh3y closed this as completed Jul 21, 2016
jh3y added a commit that referenced this issue Jul 21, 2016
@HongPong
Copy link
Author

This is great! excellent timing and works like a charm. Cheers!

@jh3y
Copy link
Owner

jh3y commented Jul 21, 2016

No problem 😄 glad I could help!

@jh3y

@HongPong
Copy link
Author

At least for me, this isn't working for IE11 (oh joy, fun times) - I can post diagnostic info if you can point me to what you might need. Thanks again. I'm not up on all the oddities that might happen with passing through prototype constructors in IE, maybe that is the issue?

@jh3y
Copy link
Owner

jh3y commented Jul 30, 2016

Hey @HongPong

Sorry for the delayed response. I sometimes miss the notifications. Hmm I ran it in Edge with no problems and IE9.

You definitely pulled in jQuery to leverage the .animate function?

@jh3y

@HongPong
Copy link
Author

HongPong commented Aug 2, 2016

I found a different way to work around it. Best regards, thanks again for all the help.

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

2 participants