jquery plugin use class name as trigger and data attributes for options

Hi,

I want to create plugins in the same way as the malsup cycle2 plugin eg

<div class="cycle-slideshow"
    data-cycle-fx="scrollHorz"
    data-cycle-pause-on-hover="true"
    data-cycle-speed="200"
    >
    <img src="http://malsup.github.io/images/p1.jpg">
    <img src="http://malsup.github.io/images/p2.jpg">
    <img src="http://malsup.github.io/images/p3.jpg">
    <img src="http://malsup.github.io/images/p4.jpg">
</div>

Open in new window


rather than having to create the options in my JS file and auto-initialize the plugin when the page is loaded.

Any tips much appreciated!
daisydoosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
No problem. jQuery can read data- attributes quite easily with $.data().

https://api.jquery.com/data/
0
daisydoosAuthor Commented:
Yes I know that, how do initialize the plugin without having to do it on page load in the JS - as the Cycle2 plugin does, and which is the question !
0
Tom BeckCommented:
You think you are being clear with your question? It sounded to me that you wanted to create your own plugin and use the data- attribute to set parameters.
I want to create plugins in the same way as the malsup cycle2 plugin
How would you like to initialize the plugin if not on load? By button click?

If the plugin is not initialized on load then what displays in its place? Nothing? A place holder?

Cycle 2 provides documentation. You could start there.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

daisydoosAuthor Commented:
HI,

Sorry for not being clearer - I apologize.

As with the Cycle2 plugin I don't want to have to call the plugin in JS, I want everything done from the mark up so there is no need for any JS at all in relation to the plugin. Unlike Cycle2 however, I want the plugin to be called on a click - not to be auto initialised on page load. The fresco.js lightbox also does this.
0
Tom BeckCommented:
You could hide all slideshows to begin.
#slideshow1 {
   display:none
}
Then in the HTML, display the slideshow.
<button onClick="document.getElementById('slideshow1').style.display='block';">Show Slideshow One</button>
<button onClick="document.getElementById('slideshow1').style.display='none';">Hide Slideshow One</button>
<div id="slideshow1" class="cycle-slideshow"
    data-cycle-fx="scrollHorz">
    <img src="http://malsup.github.io/images/p1.jpg">
    <img src="http://malsup.github.io/images/p2.jpg">
    <img src="http://malsup.github.io/images/p3.jpg">
    <img src="http://malsup.github.io/images/p4.jpg">
</div>

Open in new window

Of course with that method, the slideshow is still being initialized and is running, just not visible.

To have them initialized hidden and not cycle until a button was clicked would require a small amount of jQuery.
$(function(){
	$('#slide1Toggle').click(function(){
		$('#slideshow1').toggle();
		if ($('#slideshow1').css('display') === 'block') {
			$('#slideshow1').cycle('resume');
		} else {
			$('#slideshow1').cycle('pause');
		}
	});
});

Open in new window

HTML
<button id="slide1Toggle">Toggle Slideshow One</button>
<div id="slideshow1" class="cycle-slideshow"
    data-cycle-fx="scrollHorz"
    data-cycle-cmd="pause">
    <img src="http://malsup.github.io/images/p1.jpg">
    <img src="http://malsup.github.io/images/p2.jpg">
    <img src="http://malsup.github.io/images/p3.jpg">
    <img src="http://malsup.github.io/images/p4.jpg">
</div>

Open in new window

0
daisydoosAuthor Commented:
Thanks - have sussed it from the malsup code

$(document).ready(function() {
    $( $.fn.cycle.defaults.autoSelector).cycle();
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.