Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: [DHTML] Rainbow link on mouseOver

  1. #1
    Join Date
    Aug 2007
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [DHTML] Rainbow link on mouseOver

    1) CODE TITLE: Rainbow link on mouseOver

    2) AUTHOR NAME/NOTES: Thái Cao Phong

    3) DESCRIPTION: Implement this script into your web pages to make all links change color by themself on mouse over event. Colors defined by an array with hexadecimal value, clrArray, you can change and add colors easily. All you need to do is cope all JavaScript code and paste into your web pages, you should save them into a file for using this effect on many pages.

    4) URL TO CODE: http://javascriptbank.com/javascript...mouseover.html

    or, ATTACHED BELOW (see #3 in guidelines below):

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think DD already has a script like this, not sure*. Anyways:

    Code:
    <script language=javascript>
    has been deprecated in favor of the type attribute:

    Code:
    <script type="text/javascript">
    Your code intentionally skips Opera, but could easily work in it, and uses browser sniffing rather than feature detection to decide its execution pathway.


    * http://www.dynamicdrive.com/dynamicindex5/rainbow.htm
    Which also has some problems, but does work in Opera, IE, FF, Safari (win).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Blarg, I rewrote this whole thing and then closed my browser tab by accident. The code is horribly written (probably in the DD version as well). Here's v1v2:
    Code:
    <script type="text/javascript">
      Array.prototype.filter = function(f) {
        f = f || function(a) { return a; };
    
        for(var i = 0, n = this.length, r = []; i < n; ++i)
          if(f(this[i]))
            r.push(this[i]);
    
        return r;
      };
    
      var RainbowElements = {
        classRegex: /rainbow\(([^\)]+)\)/,
        init: function(elms) {
          for(var i = 0, a = elms, m, n = a.length; i < n; ++i)
            if(m = a[i].className.match(RainbowElements.classRegex))
              RainbowElements.setup(a[i], m[1].split(/\s*,\s*/));
        },
        setup: function(el, colours) {
          var
            cptr = 0,
            rh,
            nextColour = function(el) {
              el.style.color = colours[++cptr] || colours[cptr = 0];
            },
            resetColour = function(el) {
              el.style.color = colours[cptr = 0];
            };
    
          el.onmouseover = function() {
            rh = setInterval(function() { nextColour(this); }, 200);
          };
    
          el.onmouseout = function() {
            resetColour(this);
            clearInterval(rh);
          };
    
          resetColour(el);
    
          el = null;
        }
      };
    
      onload = function() { RainbowElements.init(document.links) };
    </script>
    <!-- ... -->
    <a href="foo" class="rainbow(red, #ffa500, yellow, #0f0, blue, #4b0082, #ee82ee)">Bar</a>
    Untested.
    Last edited by Twey; 11-23-2007 at 08:39 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Untested.
    Unworking:

    Error: RainbowLinks is not defined
    Source File: file://localhost/C:/webwork2/test/twey_rainbow.htm
    Line: 24
    Code:
    if(m = a[i].className.match(RainbowLinks.classRegex))
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops! I generalised it and forgot to change the references. Edited.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I noticed that and fixed it myself, but then I got:

    Error: el.style has no properties
    Source File: file://localhost/C:/webwork2/test/twey_rainbow.htm
    Line: 32
    Code:
    el.style.color = colours[++cptr] || colours[cptr = 0];
    Which I am also now getting with your edited version (which, BTW you changed exactly as I had to fix the first error). Why not just debug the whole thing?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I noticed that and fixed it myself, but then I got:
    Yep, I made another daft mistake: should look like this:
    Code:
              el.onmouseover = function() {
                var me = this;
                rh = setInterval(function() { nextColour(me); }, 100);
              };
    After this modification it works, but I added some more features too.
    Why not just debug the whole thing?
    Generally my code works first-time, and I've developed a habit of not actually testing it Nevertheless, I added some features and tested them fully this time:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Rainbow Text Demo Page</title>
        <script type="text/javascript">
          Array.prototype.filter = function(f) {
            f = f || function(a) { return a; };
    
            for(var i = 0, n = this.length, r = []; i < n; ++i)
              if(f(this[i]))
                r.push(this[i]);
    
            return r;
          };
    
          var RainbowElements = {
            classRegex: /rainbow\(([^\)]+)\)/,
            init: function(elms) {
              for(var i = arguments.length - 1; i >= 0; --i)
                for(var a = arguments[i], j = a.length - 1, m; j >= 0; --j)
                  if(m = a[i].className.match(RainbowElements.classRegex))
                    RainbowElements.setup(a[j], m[1].split(/\s*,\s*/));
            },
            getNamedArg: function(lst, prop, def) {
              for(var i = lst.length - 1; i >= 0; --i)
                if(lst[i].indexOf(prop + ":") === 0)
                  return lst.splice(i, 1)[0].substr((prop + ":").length);
    
              return def;
            },
            setup: function(el, colours) {
              var
                prop = RainbowElements.getNamedArg(colours, "property", "color"),
                delay = RainbowElements.getNamedArg(colours, "delay", 100),
                cptr = 0,
                rh,
                nextColour = function(el) {
                  el.style[prop] = colours[++cptr] || colours[cptr = 0];
                },
                resetColour = function(el) {
                  el.style[prop] = colours[cptr = 0];
                };
    
              el.onmouseover = function() {
                var me = this;
                rh = setInterval(function() { nextColour(me); }, delay);
              };
    
              el.onmouseout = function() {
                resetColour(this);
                clearInterval(rh);
              };
    
              resetColour(el);
    
              el = null;
            }
          };
    
          onload = function() { RainbowElements.init(document.links) };
        </script>
      </head>
      <body>
        <p>
          <a
           href="foo"
           class="rainbow(delay:50, red, #ffa500, yellow, #0f0, blue, #4b0082, #ee82ee)">
            Bar
          </a>
      </body>
    </html>
    A class now contains a sequence of colours, but may also contain anywhere an argument:value pair. Two exist: delay and property. E.G., to have the background colour change colour instead of the foreground colour, we could write:
    Code:
    class="rainbow(red, lime, property:backgroundColor, blue)"
    Or, for a smooth fading effect:
    Code:
    class="rainbow(#00f, #20d, #40c, #60a, #808, #a06, #c04, #e02, #f00, #e02, #c40, #a60, #880, #6a0, #4c0, #2e0, #0f0, #0e2, #0c4, #0a6, #088, #06a, #04c, #02e, delay:50)"
    Last edited by Twey; 11-23-2007 at 11:45 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Yep, there is a script on DD similar to this already. One of those 90's scripts that to be honest, I would have promptly dismissed if we met today.

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Generally my code works first-time, and I've developed a habit of not actually testing it Nevertheless, I added some features and tested them fully this time
    Something akin to working the NYT Sunday crossword in pen, eh? Anyways, adding features that require verbose inline code isn't generally the best way to go, and doesn't really make up for not proofreading the original effort. I'll give you a pass though because you did say that it wasn't tested.

    But don't throw away your pencil just yet.

    Quote Originally Posted by ddadmin View Post
    Yep, there is a script on DD similar to this already. One of those 90's scripts that to be honest, I would have promptly dismissed if we met today.
    At least that one, as I noted in my first post in this thread, works pretty well cross browser. It is (as I also noted) fairly atrociously coded, though somewhat understandably so, given when and for which browsers it was written.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Anyways, adding features that require verbose inline code isn't generally the best way to go
    It doesn't require verbose inline code at all. It uses classes to allow options to be set per element. This is by far preferable to having a global options set elsewhere, or even something like an ID/options hash, since it's easier to read all round. I suppose it could save some time to have a class/options hash, so I've implemented one of those (as well as giving some more subtle examples than an actual rainbow).
    But don't throw away your pencil just yet.
    I also don't test because the majority of my code is just to demonstrate a technique, and could as easily be pseudo-code. You're right that I should test more than I do, though.
    Last edited by Twey; 11-24-2007 at 09:43 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •