Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Build a Toggling Announcement Slider Using MooTools 1.2

20 Responses »

A few of my customer have asked for me to create a subtle but dynamic (...I know...) way for them to advertise different specials on their website. Not something that would display on every page, but periodically or only the homepage. Using a trick from GoDaddy's playbook, I put together an announcement slider that toggles on click. Thanks to MooTools 1.2, this was a breeze.

Toggle Announcement

Let me show you how to take advantage of the toggling announcement.

The CSS

#coupon			{ top:0; right:100px; position:absolute; cursor:pointer; width:585px; background:url(slider-coupon.png) 0 bottom no-repeat; }
.closed			{ height:29px; }
.open			{ height:176px; }

As you can see, I'm using the coupon image as a background image to a DIV I'll create using MooTools. The top and right properties allow me to position the coupon and I added the pointer cursor to let users know they need to click on the element. The heights set in the open and closed classes will cover the entire height image and just the "tab" part of the image.

The MooTools JavaScript

window.addEvent('domready', function() {
			
	//inject div
	var el = new Element('div', {
		'id': 'coupon',
		'class': 'closed',
		'text': ' '
	}).inject(document.body);
	
	//settings
	var state = 'closed';
	
	//add the click toggle
	el.addEvent('click', function() {
		
		//change the state
		state = (state == 'closed' ? 'open' : 'closed');
		el.morph('.' + state);
		
	});
	
});

The code is actually pretty simple. Once the DOM is ready, I inject the coupon DIV in to the body of the page. Then, I create a setting that will help me keep track of the slider's state. I then add a click event to the couponDIV that changes the element's state and morphs to the appropriate open / closed class appropriately. That's it!

More Toggler Ideas

A few more ideas for the toggler:

  • The toggler could use mouseenter and mouseleave events just as easily
  • You could use this functionality as a preferences "dashboard." For example, I could use this on each article page. The toggler would have checkboxes that said "Hide Trackbacks" or "Hide Images".
  • I'd recommend only using this for advertising on one or very few pages. Having this on every page would annoy the hell out of people.

Discussion

  1. July 14, 2008 @ 10:12 am

    Simple and elegant. I like it!

  2. July 14, 2008 @ 10:31 am

    Nice…

  3. July 14, 2008 @ 4:05 pm

    Nice one but beware, you’re over your search input / button on the demo page, because your div is larger than the event button that fire the announcement. I know that this is just a demo page, but this should be avoided by anyone that is using this type of component, be sure to not overlay on things.

    Keep the good work ! ;)

  4. July 14, 2008 @ 6:34 pm

    Just awesome!
    Well done :)

  5. July 15, 2008 @ 3:00 am

    Hi!

    What about if javascript is turned off but css is on? Maybe adding:

    coupon{display:none;top:0; right:100px; position:absolute; cursor:pointer; width:585px; background:url(slider-coupon.png) 0 bottom no-repeat;}

    and then change this rule via javascript to display:inline solve the problem.

    Sorry for my inglish.

  6. July 15, 2008 @ 7:10 am

    @Bleyder: Good idea!

  7. speed
    August 15, 2008 @ 9:08 am

    Hey David thanks for your 1.2 tutorials a lot :)

    I need a little help from you because I’m still learning JS so I can’t figure it on my own :(
    here’s the script : http://demos.mootools.net/Fx.Slide
    All I want is to remove all the links for the div and just leave “toggle”

    like right now there are : “slide out | slide in | toggle | hide | show”
    but when I remove the the “slide in” and “slide out” the script stops working :( How can I just put “toggle” and remove rest of the links and still make the slider work?

    Thanks!

  8. speed
    August 16, 2008 @ 2:27 am

    Ok cool so I’ve managed to achieve what I was talking about up there ^^

    But now I need something else…. The status of the panel won’t update until I have the “Show” and “Hide” tabs in the HTML :(

    How can I just put “toggle” in the html and make the status change “open and close” ?

    Thanks :)

  9. speed
    August 30, 2008 @ 3:19 pm

    David you never came back to reply :(

    Sorry for being so demanding lol!

  10. July 16, 2009 @ 4:48 pm

    Awesome. Thanks! With a little modification, it was just what I was looking for to ascertain if a toggled element was open or closed. Keep up the good work!

  11. peter
    July 17, 2009 @ 12:24 pm

    Hello,

    Is a Toggling Announcement Slider free for commercial use ?

    Thanks in advance.

    Peter

  12. July 17, 2009 @ 12:34 pm

    Peter: Do whatever you’d like with it! :)

  13. monjur ahmed
    August 17, 2009 @ 2:53 am

    Very nice example. You know Joomla 1.5 uses MooTools 1.11. I tried to port this example in Joomla 1.5, but failed.
    I am looking forward for your another blog entry that describes how to do it in Joomla 1.5.

  14. dino
    September 17, 2009 @ 3:04 am

    hi david is there any way to make it come from right to left?

  15. November 7, 2009 @ 1:02 pm

    Hi David – any chance that you could publish the jquery version of this?

    PS – thanks for all the help you have given me without even knowing it. Your blog has proved invaluable to me. Al

  16. mae
    January 15, 2010 @ 4:18 am

    hey david,

    the demo seems not to work anymore, bye

    @ironsideboy: tehere are thousand copies of this script @ jquery downloadbase

  17. matt
    February 24, 2010 @ 3:32 pm

    Seems your fixed position search bar covers up your demo.

  18. March 11, 2010 @ 10:02 pm

    If you are willing to buy a house, you will have to receive the home loans. Furthermore, my father always uses a college loan, which occurs to be the most reliable.

  19. August 18, 2010 @ 2:55 am

    Hi David – any chance that you could publish the jquery version of this?

    PS – thanks for all the help you have given me without even knowing it. Your blog has proved invaluable to me. Al

  20. August 19, 2010 @ 9:46 am

    Ok cool so I’ve managed to achieve what I was talking about up there ^^

    But now I need something else…. The status of the panel won’t update until I have the “Show” and “Hide” tabs in the HTML :(

    How can I just put “toggle” in the html and make the status change “open and close” ?

    Thanks :)

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!