Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Pat 56 posts 147 karma points
    Jan 27, 2012 @ 12:47
    Pat
    0

    Mouse over image changing

    Hi,

    Been searching can't find anything though, is there a way to change the image in my getMedia code to change the image when there mouse hovers over it?

    Current code is :

    <xsl:variable name="mediaId" select="iconServices" />
       <xsl:if test="$mediaId > 0">
          <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
          <xsl:if test="$mediaNode/umbracoFile">
          <img src="{$mediaNode/umbracoFile}" />
       </xsl:if>
    </xsl:if>

  • Jon 36 posts 59 karma points
    Jan 27, 2012 @ 12:58
    Jon
    0

    Are you trying to change the image displayed on an 'img' element when hovering over that element?

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jan 27, 2012 @ 12:59
    Chriztian Steinmeier
    0

    Hi Pat,

    As you probably know, XSLT will not receive any mousevents, so you need to think about what the final HTML should be - usually, something like this uses an image with a pre- or postfix (e.g. "imageName_over.png"), having the effect handled from JavaScript or even better - with CSS.

    Then you need to think about how to specify the image in the backend (it seems like that's also something you'd want) - you could add it as another Upload field on the Image Media Type

    If you need JavaScript to take the hover-image from the HTML you could put the URL in a data-attribute, e.g.:

    <img src="{$mediaNode/umbracoFile}" data-hoverimg="{$mediaNode/hoverFile}" />

    You can then grab the hoverImage using jQuery's data() method:

    var hoverImage = $image.data('hoverimg'); // $image is a jQuery object pointing to the <img>

    Personally, I'd use CSS for this - but it obviously depends on the situation.

    /Chriztian 

     

  • Jon 36 posts 59 karma points
    Jan 27, 2012 @ 13:08
    Jon
    1

    @Chriztian I was going to offer similar advice :)

    There is another way you can do it, if you produce the following mark-up:

    <img onmouseover="this.src='{URL of hover image}'" onmouseout="this.src='{URL of image}'" src="{URL of image}" />

    You can use the GetMedia function to get the 'URL of image' and 'URL of hover image' to populate the element attributes.

    This is good if you need a really quick and simple fix, but like Chriztian, I personally prefer the CSS based approach for the cleaner mark-up.

  • Pat 56 posts 147 karma points
    Jan 27, 2012 @ 13:42
    Pat
    0

    Can the above be implemented in the same get media function or is it a case or having another one?

    The code in my first post gets the image and then a macro pulls that through to a parent page.

  • Jon 36 posts 59 karma points
    Jan 27, 2012 @ 15:40
    Jon
    0

    Like Criztian said above, you'll need to think about how you specify the hover image in the back end. Does your image come from a specific Document Type, can you add another property to it to allow you to pick the hover image?

    It's difficult to give more specific advice without knowing how this is macro is used in your parent page, the XSLT above seems to be supplying you with a complete 'img' element based on another variable passed in called 'iconServices'. Is icon services a content item? If so, then you could add the hover image property to the Document Type for this item, then your macro can build the 'img' element as above with the both the onmouseover and onmouseout attributes set.

  • Pat 56 posts 147 karma points
    Jan 27, 2012 @ 16:00
    Pat
    0

    I have a parent page (Services), then several child pages (service 1, 2, 3 etc). The XSLT picks the title, content and image from the child pages and then the macro is pulled through on my parent services page, this generates a list of services on the parent page. They all have the same image as I have given them the same one, but I will be turning them into links at some point but when I do this I want the image to be replaced by a different image (well, on rollover).

  • Jon 36 posts 59 karma points
    Jan 27, 2012 @ 16:25
    Jon
    0

    Which version of Umbraco are you using?

  • Pat 56 posts 147 karma points
    Jan 27, 2012 @ 16:30
    Pat
    0

    v4.7.1.1 is what I am using.

  • Pat 56 posts 147 karma points
    Jan 31, 2012 @ 12:13
    Pat
    0

    Any way of using this within XSLT code itself? Using imagegen too if that helps?

    Code I have so far is below:

    <xsl:variable name="mediaId" select="iconServices" />
    <xsl:if test="$mediaId > 0">
    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
    <xsl:if test="$mediaNode/umbracoFile">
    <img src="/imageGen.ashx?image={$mediaNode/umbracoFile}&width={$mediaNode/umbracoWidth}&height={$mediaNode/umbracoHeight}" height="{$mediaNode/umbracoHeight}" width="{$mediaNode/umbracoWidth}" />
    </xsl:if>
    </xsl:if>

  • Jon 36 posts 59 karma points
    Jan 31, 2012 @ 16:10
    Jon
    0

    The issue with the code above is that it looks like you are still dealing with only one media item. You are getting a single media id from iconServices and the requesting that item from the Media library, so you will only ever have a single media item to display (so you won't be able to specify a hover image in the 'img' element).

    This is the simplest way to achieve what you are looking to do (this is what I did earlier):

    1. Have 2 media picker properties on your Document Type for your 'Service' child pages, 1 for your image one for your Hover image - this will allow you to specify the normal and hover images to use

    2. Have something like this in your XSLT:

    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">  
      <xsl:if test="./image &gt; 0 and ./imageHover &gt; 0">
        <xsl:variable name="imageNode" select="umbraco.library:GetMedia(./image, 0)" />
        <xsl:variable name="hoverImageNode" select="umbraco.library:GetMedia(./imageHover, 0)" />
        
        <img src="{$imageNode/umbracoFile}"
            onmouseover="javascript:this.src='{$hoverImageNode/umbracoFile}';"
            onmouseout="javascript:this.src='{$imageNode/umbracoFile}';" />
        <br />
      </xsl:if>
    </xsl:for-each>

    (Note theat my properties on the document type are 'image' and 'imageHover', you'll need to replace with your own)

    3. Add the macro that uses this XSLT to your template for your parent page

    You may need to tweak the error checking for this as at the moment it ensures that both image and hover image fields are populated (to avoid errors), but you get the idea.

Please Sign in or register to post replies

Write your reply to:

Draft