0

Basically, I have a number of icons and I want the content above those icons to change when someone hovers over the icon. My plan at the moment is to use mouseover to add/remove a class that changes the opacity of the div and just have the divs overlap each other with absolute positioning. Because I'm a self-taught developer, I find, looking back, that I usually make things harder on myself than they need to be. :) I figured I would ask some of the communities I'm apart of for their opinion on this setup. Maybe there is a better way I'm totally missing. I need it to be compatible back to IE8.

enter image description here

Edit: I'm using jQuery 1.8.3. Sorry for leaving that out.

jphogan
  • 157
  • 1
  • 15

3 Answers3

1

Your question was kind of vague. You used the tag 'jquery' so I'm assuming you are using some version of jquery on your site.

I did up a quick jsfiddle for you: http://jsfiddle.net/pMCxP/2/

Instead of using the mouseover event, I have used the hover event.

$(this).hover(function(){
        $("#content div").hide();
        var contentBlock = $(this).attr('class');
        $("#" + contentBlock).show();
    });

In the code, I have given each content block an id, then for the corresponding button/icon I have set it's css class to be the same as the id for the content. Then I can use it to show the block on hover.

I hope that helps you out, or at least points you in the right direction.

Tyanna
  • 719
  • 6
  • 11
  • Thank you for taking the time to respond and mock that up. I really appreciate it. I apologize for being vague, I've edited my post a bit now. Do you foresee any problems with Screen Readers and/or SEO by setting it to display:none? – jphogan Jul 02 '13 at 18:19
  • @jphogan ~ that's a good question. I normally work on closed network sites so SEO isn't something I have to worry about. A quick search gave me an answer over on the SE site Webmasters. The hit only happens if there isn't a mechanism to show the div, which there is in this case: http://webmasters.stackexchange.com/questions/1377/how-bad-is-it-to-use-display-none-in-css – Tyanna Jul 02 '13 at 18:24
  • Thanks for the response and linking to that information. I appreciate it! – jphogan Jul 02 '13 at 19:27
  • @jphogan ~ NP. :) Don't forget to accept one of the answers for your question. – Tyanna Jul 03 '13 at 21:13
1

Here is a method that allows you to predefine the html and css for each "icon" mouse-over.

Live version: http://jsfiddle.net/gSAjV/2240/

var content1 = '<p>This is some html that will fill the content window</p>';
var content2 = '<p>This is more html that will fill the content window</p>';
var content3 = '<p>This is other html that will fill the content window</p>';
var content4 = '<p>This is even more html that will fill the content window</p>';

$('#icon1').mouseover(function(){
    $('#content').html(content1);
    $('#content').css( "background-color", "red" );
});
$('#icon2').mouseover(function(){
    $('#content').html(content2);
    $('#content').css( "background-color", "orange" );
});
$('#icon3').mouseover(function(){
    $('#content').html(content3);
    $('#content').css( "background-color", "yellow" );
});
$('#icon4').mouseover(function(){
    $('#content').html(content4);
    $('#content').css( "background-color", "green" );
});

This works, but to my knowledge it is far more difficult to add clean transitions when you are changing the div content. If you want to have fades or any other "A"-over-"B" type transitions, then it may be better to have all of your content preloaded on multiple, layered divs as you suggested.

It is also possible to make transitions using only two divs. One div for holding your current content and one that loads the new content on mouseover and once it is loaded, fades (transitions) in.

Enigmadan
  • 3,398
  • 2
  • 23
  • 35
  • This is great! Thanks for responding and mocking that up. By having the html generated by javascript, do you think there would be an negative SEO effects and/or consequences for screen readers? I like this solution though. – jphogan Jul 02 '13 at 18:22
  • Yes. The content would not appear in searches because it isn't on the page and screen readers can't read what isn't there. But then again, mouseover isn't an altogether screen reader friendly method of navigation. – Enigmadan Jul 02 '13 at 18:27
  • I'm considering the case where you use an external js file. If the jquery is on the page itself, then I'm not sure... I believe that screen readers skip ` – Enigmadan Jul 02 '13 at 18:30
  • Some Javascript & SEO research: [SEO & Javascript DOs and DON'Ts](http://www.developerdrive.com/2012/08/seo-and-javascript-dos-and-donts/) and [Javascript & SEO info/suggestions](http://www.seomarketingworld.com/seo-faq/javascript.php) – Enigmadan Jul 02 '13 at 18:37
  • Last one. Some Javascript/screen reader suggestions: http://stackoverflow.com/questions/3670570/getting-screen-reader-to-read-new-content-added-with-javascript And an amazing list of javascript functions and their accessibility by screenreaders: http://northtemple.com/2008/10/07/javascript-and-screen-readers – Enigmadan Jul 02 '13 at 18:53
  • Wow. Thanks for doing all that research and posting it. I had similar concerns about using mouseover. I thought maybe using "opacity" rather than "display:none;" would do the trick, but I like your solution a lot. Thanks again! – jphogan Jul 02 '13 at 19:26
  • You could try doing `display:none;` but on any event change where a div with new content is visible have a `aria-live="polite"` call. That should let any screen reader know that there is new information to be read. [ARIA live Doc](http://www.w3.org/TR/wai-aria/states_and_properties#aria-live), [How to use the "live" property](http://www.w3.org/WAI/PF/aria-practices/#liveprops) – Enigmadan Jul 02 '13 at 19:55
1

I would build like this

http://jsfiddle.net/UQPe3/1/

HTML

<div class='container'>
    <div class='slide1'>slide1</div>
    <div class='slide2 hidden'>slide2</div>
    <div class='slide3 hidden'>slide3</div>
    <div class='slide4 hidden'>slide4</div>
</div>

<div class='icon-wrapper'>
    <div class='icons'>
        <button type='button' data-slide='slide1'></button>
        <button type='button' data-slide='slide2'></button>
        <button type='button' data-slide='slide3'></button>
        <button type='button' data-slide='slide4'></button>
    </div>
</div>

CSS

.container div {
    background: #999;
    height: 300px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 20px;
    text-align: center;
    width: 600px;
}

.icon-wrapper {
    text-align: center;
}

.icons {
    display:inline-block;
}

.icons button {
    background-color: #999;
    border: none;
    cursor: pointer;
    height: 60px;
    margin-left: 30px;
    margin-right: 30px;
    width: 60px;
}

.icons button:hover {
    background-color: #333;
}

.hidden {
    display: none;
}

JS

$('.icons button').on(
{
    mouseenter: function()
    {
        $('.container div').addClass('hidden');
        var show_slide = $(this).attr('data-slide');
        $('.' + show_slide).removeClass('hidden');
    }
});
djrconcepts
  • 635
  • 5
  • 6
  • Thanks for the response and the mockup. I like this solution. I was originally worried about using display:none for screen reader and SEO purposes, but some other posted helpful links indicating that wasn't as detrimental as I'd feared. Thanks again! – jphogan Jul 02 '13 at 19:47