Morphext icon indicating copy to clipboard operation
Morphext copied to clipboard

Stop after completed

Open mrkenng opened this issue 7 years ago • 6 comments

Is there a way to stop the loop? I tried using complete function but I guess that is for each item after completed am I right?

mrkenng avatar May 05 '17 09:05 mrkenng

Yes, that's the general recommendation @mrkenng. Bear in mind that you can call the stop outside the complete handler too. For example:

var morphext = $("#js-rotating").Morphext({
   animation: "rotateIn"
});
var data = morphext.data("plugin_Morphext");

// Start Morphext (autostarts by default)
data.start();

// Stop Morphext
data.stop();

MrSaints avatar May 05 '17 10:05 MrSaints

Here's it what I am attempting:

var morphistAnim = $("#fadeInOut").Morphist({
		animateIn: "fadeIn",
		animateOut: "fadeOut",
		speed: 2000
	});
	var data = morphistAnim.data("plugin_Morphist");

	if (window.matchMedia("(min-width: 48em)").matches){
		$("#fadeInOut").addClass('morphist');
		data.start();		
	} else {
		data.stop();
		$("#fadeInOut").removeClass('morphist');
	}
	$(window).resize(function() {
		var morphistAnim = $("#fadeInOut").Morphist({
			animateIn: "fadeIn",
			animateOut: "fadeOut",
			speed: 2000
		});
		var data = morphistAnim.data("plugin_Morphist");
		console.log(data);
		if (window.matchMedia("(min-width: 48em)").matches){
			$("#fadeInOut").addClass('morphist');
			data.start();			
		} else {
			data.stop();
			$("#fadeInOut").removeClass('morphist');
		}
	});

Narrow width before breakpoint should be no Morphist. Once breakpoint is reached, apply and run Morphist animation. The opposite should work if viewport width goes back down to narrow. I'm close, but getting TypeError t.start is not a function when viewport gets wider and passes the breakpoint.

tomliv avatar Mar 12 '18 15:03 tomliv

@tomliv

var morphistAnim = $("#fadeInOut").Morphist({
  animateIn: "fadeIn",
  animateOut: "fadeOut",
  speed: 2000
});
var data = morphistAnim.data("plugin_Morphist");

// Stop it by default
// There is likely a more elegant way of doing this.
// For example, some sort of singleton to only run the above when we need to,
// and ensuring we do not call stop if it never started (otherwise, there'll be no instance).
data.stop();

function toggleMorphist() {
  if (window.matchMedia("(min-width: 48em)").matches) {
    data.start();
  } else {
    data.stop();
  }
}

$(window).resize(function() {
  toggleMorphist();
});

I believe something like the above should work. Haven't actually tested it.

MrSaints avatar Mar 12 '18 17:03 MrSaints

Thanks! I'll give it a run and get back to you here.

tomliv avatar Mar 12 '18 18:03 tomliv

Doesn't seem to be working. Just plays upon load - below the breakpoint - as if I am just simply initializing it. Default stop isn't working. Continues to work after resize.

tomliv avatar Mar 12 '18 18:03 tomliv

If anyone needs it, I was able to stop it from looping with this code:

complete: function () { 
      $length = this.phrases.length - 1;    
       if (this.index == $length) {       
      	  this.stop();        
       };
 }

lindsayweb avatar Oct 29 '19 20:10 lindsayweb