www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 04-15-2005, 03:01 PM
    betheball betheball is offline
    Registered User
     
    Join Date: Sep 2003
    Location: Utah
    Posts: 394
    Mouseover effects

    I am using the script below for a mouseover effect on a handful of images. I chose this one simply because of ease of implementation. However, I am noticing that sometimes an image won't load. I may be wrong, but it seems to happen when the cursor happens to be sitting where the image appears. Has anyone ever seen that type of a problem? Also, what are your recommendations for a dependable mouseover script.

    Here is the script:

    Code:
    /****************************************************
    *	        DOM Image rollover:
    *		by Chris Poole
    *		http://chrispoole.com
    *               Script featured on http://www.dynamicdrive.com
    *		Keep this notice intact to use it :-)
    ****************************************************/
    
    function init() {
      if (!document.getElementById) return
      var imgOriginSrc;
      var imgTemp = new Array();
      var imgarr = document.getElementsByTagName('img');
      for (var i = 0; i < imgarr.length; i++) {
        if (imgarr[i].getAttribute('hsrc')) {
            imgTemp[i] = new Image();
            imgTemp[i].src = imgarr[i].getAttribute('hsrc');
            imgarr[i].onmouseover = function() {
                imgOriginSrc = this.getAttribute('src');
                this.setAttribute('src',this.getAttribute('hsrc'))
            }
            imgarr[i].onmouseout = function() {
                this.setAttribute('src',imgOriginSrc)
            }
        }
      }
    }
    onload=init;
    Here is the code to display the images:

    Code:
    <img src="../../images/back.gif" alt="Back" hsrc="../../images/back_mo.gif" border="0">
    Reply With Quote
      #2  
    Old 04-15-2005, 03:23 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    Maybe because the script is pre-loading all the hsrc's and not preloading all the regular src's...

    It's a pretty cool script though...
    My favorite part is how the function checks if the function document.getElementById exists then assumes that the function
    document.getElementsByTagName also exists if the first one does...
    That is not always true! The function in my signature can identify the first function and make the whole program glitch! LOL
    The script never even uses the function document.getElementById...
    I'd change the orginal programmer's if-check to document.getElementsByTagName

    Let me guess you're using IE
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.
    Reply With Quote
      #3  
    Old 04-15-2005, 03:38 PM
    betheball betheball is offline
    Registered User
     
    Join Date: Sep 2003
    Location: Utah
    Posts: 394
    Yes, I am using IE. Also, my site is on a corporate intranet where users use IE exclusively. Are you saying replace:

    function init() {
    if (!document.getElementById) return

    with

    function init() {
    if (!document.getElementsByTagName) return

    I did that and still see the same issue. I have verified that what I though was true. If for example I refresh the page and hurry and move the mouse pointer to where one of the images needs to appear, the image will fail to load. Curiously, if I leave the cursor over the image and hit F5 it loads fine. Do you think I should choose a different script? Although I will have to redo over a hundred pages, I prefer stability. Thanks again for your continued help.
    Reply With Quote
      #4  
    Old 04-15-2005, 03:51 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    No, it's a good script. It only seems to be an issue with IE that's how I knew you were using IE.
    If you want, you can display a loading-layer on the top of everything onload and remove it after all the images are done.
    I could extend the current script your using by doing so.
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.
    Reply With Quote
      #5  
    Old 04-15-2005, 03:56 PM
    betheball betheball is offline
    Registered User
     
    Join Date: Sep 2003
    Location: Utah
    Posts: 394
    If you could, I would appreciate it. Many of the graphics have links associated with them and when a user clicks a link the same image often appears on the next page in the same position. Well, since the user had to click the image, the pointer is still sitting in the same spot causing the image to not load.
    Reply With Quote
      #6  
    Old 04-15-2005, 04:08 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    Well, I'm currently working on 5 scripts for different people... Your script is currently in line 6th place... Once I get around to it, I'll make it for you with no problem.
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.
    Reply With Quote
      #7  
    Old 04-15-2005, 04:28 PM
    betheball betheball is offline
    Registered User
     
    Join Date: Sep 2003
    Location: Utah
    Posts: 394
    Great, I will watch for it. I appreciate the assistance.
    Reply With Quote
      #8  
    Old 04-17-2005, 03:05 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    Code:
    <script type="text/javascript"><!--
    function init() {
      if (!document.getElementsByTagName) return false;
      var imgOriginSrc;
      var imgTemp = new Array();
      var imgarr = document.getElementsByTagName('img');
      for (var i = 0; i < imgarr.length; i++) {
        if (imgarr[i].getAttribute('hsrc')) {
            imgTemp[i] = new Image();
            imgTemp[i].src = imgarr[i].getAttribute('hsrc');
            imgarr[i].onmouseover = function() {
                imgOriginSrc = this.getAttribute('src');
                this.setAttribute('src',this.getAttribute('hsrc'))  
            }
            imgarr[i].onmouseout = function() {
                this.setAttribute('src',imgOriginSrc)
            }
        }
    
    	}
    hide_loading_layer()
    }
    
    function show_loading_layer(){
    if(!document.getElementById)return false;
    var screen_height=(typeof screen.height!="undefined")?screen.height:1900
    var screen_width=(typeof screen.width!="undefined")?screen.width:900
    
    var temp=""
    if(document.layers){
    temp+="<layer id=\"loading_layer\" style=\"position: absolute; left:0px; top:0px; display: inline; z-index: 900; height:"+screen_height+"; width:"+screen_width+"; background-color: #EEEEEE; color: royalblue; text-align: center;\">"
    temp+="<h1>LOADING</h1></layer>"
    }else{
    temp+="<div id=\"loading_layer\" style=\"position: absolute; left:0px; top:0px; display: inline; z-index: 900; height:"+screen_height+"; width:"+screen_width+"; background-color: #EEEEEE; color: royalblue; text-align: center;\">"
    temp+="<h1>LOADING</h1></div>"
    }
    document.write(temp)
    }
    function hide_loading_layer(){
    if(!document.getElementById)return false;
    setTimeout('document.getElementById("loading_layer").style.display="none"',100)
    }
    show_loading_layer()
    onload=init;
    //--></script>
    Untested in NS, I tested it in IE6.0

    If you want to test my code, try the following HTML in your BODY:
    Code:
    <!-- --- -->
    <img src="http://www.webdeveloper.com/forum/image.php?u=30185&dateline=1109700903" hsrc="http://www.webdeveloper.com/forum/image.php?u=2424&dateline=1105516794">
    <!-- --- -->
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.

    Last edited by Ultimater; 04-17-2005 at 04:56 PM. Reason: changed display:block; to display:inline; and position:absolute;
    Reply With Quote
      #9  
    Old 04-17-2005, 03:28 AM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    Hmm... Is it a problem if my program is setting the layer's width and height equal to the user's actual computer screen height and width rather than the page's width and height? Of corse it is, becase your page length could require scrolling and the layer wouldn't "hide" all of it's contents.
    Hmm... I'm not sure how to detect the webpage's true height and width...
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.
    Reply With Quote
      #10  
    Old 04-17-2005, 07:05 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    I think that David Harrison has thought of a better alternitive from the following thread:
    screen.Height and screen.Width

    He says to put your init function call after your html instead of before it onload.

    in-other-words:
    Code:
    <script type="text/javascript"><!--
    function init() {
      if (!document.getElementById) return
      var imgOriginSrc;
      var imgTemp = new Array();
      var imgarr = document.getElementsByTagName('img');
      for (var i = 0; i < imgarr.length; i++) {
        if (imgarr[i].getAttribute('hsrc')) {
            imgTemp[i] = new Image();
            imgTemp[i].src = imgarr[i].getAttribute('hsrc');
            imgarr[i].onmouseover = function() {
                imgOriginSrc = this.getAttribute('src');
                this.setAttribute('src',this.getAttribute('hsrc'))  
            }
            imgarr[i].onmouseout = function() {
                this.setAttribute('src',imgOriginSrc)
            }
        }
      }
    }
    //--></script>
    
    
    <img src="http://www.webdeveloper.com/forum/image.php?u=30185&dateline=1109700903" hsrc="http://www.webdeveloper.com/forum/image.php?u=2424&dateline=1105516794">
    <script type="text/javascript"><!--
    init()//omitted(left-out) onload on purpose!
    //--></script>
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.
    Reply With Quote
      #11  
    Old 04-29-2005, 12:37 PM
    betheball betheball is offline
    Registered User
     
    Join Date: Sep 2003
    Location: Utah
    Posts: 394
    Perfect! Thanks for your help on this. For some reason I didn't get my subscription email on this one. Glad I came back for another look.

    As a side note, it seems the author of the original script must have also realized the problem. The orginal has been rewritten to:

    Code:
    //=====================================================================
    //  DOM Image Rollover v3 (hover)
    //
    //  Demo: http://chrispoole.com/scripts/dom_image_rollover_hover
    //=====================================================================
    //  copyright Chris Poole
    //  http://chrispoole.com
    //  This software is licensed under the MIT License 
    //  <http://opensource.org/licenses/mit-license.php>
    //=====================================================================
    
    function domRollover() {
    	if (navigator.userAgent.match(/Opera (\S+)/)) {
    		var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);
    	}
    	if (!document.getElementById||operaVersion <7) return;
    	var imgarr=document.getElementsByTagName('img');
    	var imgPreload=new Array();
    	var imgSrc=new Array();
    	var imgClass=new Array();
    	for (i=0;i<imgarr.length;i++){
    		if (imgarr[i].className.indexOf('domroll')!=-1){
    			imgSrc[i]=imgarr[i].getAttribute('src');
    			imgClass[i]=imgarr[i].className;
    			imgPreload[i]=new Image();
    			if (imgClass[i].match(/domroll (\S+)/)) {
    				imgPreload[i].src = imgClass[i].match(/domroll (\S+)/)[1]
    			}
    			imgarr[i].setAttribute('xsrc', imgSrc[i]);
    			imgarr[i].onmouseover=function(){
    				this.setAttribute('src',this.className.match(/domroll (\S+)/)[1])
    			}
    			imgarr[i].onmouseout=function(){
    				this.setAttribute('src',this.getAttribute('xsrc'))
    			}
    		}
    	}
    }
    domRollover();
    And the instruction is to now place the script right after the </body> tag. The html portion is now:

    <img src="../../images/back.gif" class="domroll ../../images/back_mo.gif">

    Your fix was easier for me to implement with what I had already done.
    Reply With Quote
      #12  
    Old 04-29-2005, 02:56 PM
    Ultimater's Avatar
    Ultimater Ultimater is offline
    Eccentric Tatertot
     
    Join Date: Jan 2005
    Location: Los Angeles, CA
    Posts: 4,804
    Yup, that makes sense now. So, the original author also knew of the problem and you failed to carry-out his/her instructions.
    Well, feel free to sign my guestbook in return!
    http://ultimiacian.tripod.com/cgi-bin/GuestBook.pl
    Try to write something inspiring or motivating because this guestbook seems to be my only motivation. But a rather good one!
    __________________
    Ultimater

    XMLHttpRequest:
    Specification | xmlhttp object | open and onreadystatechange order | String.prototype.toXMLDocument | Sarissa | Prototype | Dojo
    Broadening one's horizons:
    24ways.org


    Note I have a bad habit of editing my posts hours at a time and hours later.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:29 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.