Web development , php , ajax , symfony, framework, zend
In: web resources
28 Dec 2009
Last week we created a very simple MooTools slideshow script. The script was very primitive: no events and no next/previous controls — just cross-fading between images. This tutorial will take the previous slideshow script a step further by:
Obviously our MooTools javascript code is going to change quite a bit so be prepared to (mostly) restructure the previous post’s javascript code.
<div id="slideshow-container"> <img src="slideshow/cricci1.jpg" alt="Christina Ricci" /> <img src="slideshow/cricci2.jpg" alt="Christina Ricci" /> <img src="slideshow/cricci3.jpg" alt="Christina Ricci" /> <img src="slideshow/cricci4.jpg" alt="Christina Ricci" /> <img src="slideshow/cricci5.jpg" alt="Christina Ricci" /> </div>
Nothing has changed from the previous post. Just images of Christina Ricci which will set your heart ablaze.
#slideshow-container { width:512px; height:384px; position:relative; }
#slideshow-container img { width:512px; height:384px; display:block; position:absolute; top:0; left:0; z-index:1; }
.toc { position:absolute; left:0; bottom:20px; z-index:2; display:block; width:20px; background:#6D84B4; color:#fff; text-align:center; padding:3px; text-decoration:none; }
.toc-active { background:#fff; color:#6D84B4; }
#next { position:absolute; bottom:20px; right:20px; z-index:2; display:block; width:20px; background:#6D84B4; color:#fff; text-align:center; padding:3px; text-decoration:none; }
#previous { position:absolute; bottom:20px; right:60px; z-index:2; display:block; width:20px; background:#6D84B4; color:#fff; text-align:center; padding:3px; text-decoration:none; }
We’ve added the toc and toc-active CSS classes. The toc-classed items will be absolutely position at the bottom of the container. We’ll calculate the left value of each item with MooTools as we don’t want them to overlap. We’ve also defined CSS rules for the Next and Previous buttons we will generate. Feel free to style the TOC items and buttons any way you’d like.
window.addEvent('domready',function() {
/* settings */
var showDuration = 3000;
var container = $('slideshow-container');
var images = container.getElements('img');
var currentIndex = 0;
var interval;
var toc = [];
var tocWidth = 20;
var tocActive = 'toc-active';
/* new: starts the show */
var start = function() { interval = show.periodical(showDuration); };
var stop = function() { $clear(interval); };
/* worker */
var show = function(to) {
images[currentIndex].fade('out');
toc[currentIndex].removeClass(tocActive);
images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
toc[currentIndex].addClass(tocActive);
};
/* new: control: table of contents */
images.each(function(img,i){
toc.push(new Element('a',{
text: i+1,
href: '#',
'class': 'toc' + (i == 0 ? ' ' + tocActive : ''),
events: {
click: function(e) {
if(e) e.stop();
stop();
show(i);
}
},
styles: {
left: ((i + 1) * (tocWidth + 10))
}
}).inject(container));
if(i > 0) { img.set('opacity',0); }
});
/* new: control: next and previous */
var next = new Element('a',{
href: '#',
id: 'next',
text: '>>',
events: {
click: function(e) {
if(e) e.stop();
stop(); show();
}
}
}).inject(container);
var previous = new Element('a',{
href: '#',
id: 'previous',
text: '<<',
events: {
click: function(e) {
if(e) e.stop();
stop(); show(currentIndex != 0 ? currentIndex -1 : images.length-1);
}
}
}).inject(container);
/* new: control: start/stop on mouseover/mouseout */
container.addEvents({
mouseenter: function() { stop(); },
mouseleave: function() { start(); }
});
/* start once the page is finished loading */
window.addEvent('load',function(){
start();
});
});
I told you it changed quite a bit, didn’t I? Here’s what I added:
The added code blocks were very simple to add; taking them one at a time kept creating these enhancements quick.
The next step will be transforming the above MooTools javascript code into a class. Class-ifying the code will make it cleaner and more organized.
Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!
Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!
Create a Simple Slideshow Using MooTools, Part II: Controls and Events
Related posts:
This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.
Copyright © 2007 - Programming Blog - is proudly powered by WordPress | Log in
Compositio Theme is created by: Design Disease brought to you by PremiumThemes.com
6 Responses to Create a Simple Slideshow Using MooTools, Part II: Controls and Events
Inacious
January 24th, 2010 at 6:57 am
Well, the market was closed 1/1/09, remember.
Chris G
February 9th, 2010 at 4:06 pm
Search engines don't index elements. When the page is spidered the tags would be removed (and possibility the content within the tag ie: <script>) and the straight text parsed and indexed. Otherwise scripts, styles, etc would be indexed and yeild unfavourable results.
They could however use them to navigate the page and well formed HTML will parse better and yield better results. Image tags could be indexed for image searches and links could be spidered.
cks
February 10th, 2010 at 7:28 pm
The reason that this code isn't working is because you aren't telling the computer where the image is exactly located. When inserting an image you really need to tell the computer what folder it is in and the extension of the image. For example: say that you had a web site on computers and you had all of your information inside of a folder called computers including your images, you would need to make a folder called images inside of the computers folder and then the code should work.
<img src="computers/images/picture.jpg"> and in your example of <img src="$image"/>; your /> is what you use to close a tag so that is probably confusing the computer also. Good luck, I hoped this helped and if you need any more help you can ask me again.
Mr. Sunshine
March 17th, 2010 at 11:34 am
I would advertise on the internet, radio, and TV.
Ron
March 27th, 2010 at 7:50 pm
Under the CSS selector name for "scroll3":
#scroll3 {
width: 572px;
height: 401px;
border: 1px solid #646464;
overflow: auto;
text-align: center;
background: url(path to image) no-repeat center scroll;
}
Make sure the width and height is large enough to put the image in as a background if image does not repeat. Add the position code as it was truncated by YA when you posted. Ignore other Answerers as the style="" attribute should not be used if you have an id selector name. Plus, you forgot to add the dimensions to the width/height so it wouldn't have worked.
Now…
<div id="scroll3"><p>1</p></div>
Ron
shooli
June 25th, 2010 at 7:21 am
You need a Photo Editing Software to do what you want.