Henry Edward Quinn IV
Henry Edward Quinn IV

Reputation: 633

How can I swap out text in a text-box by using only CSS mouse related events?

The way I have my page set up right now is pretty solid. I have a gallery of pictures, and when your mouse is over top of one of the small icons, a larger one pops up and the largest image on the page changes to that image. I did this all using onMouseOver and onMouseOff events via inline CSS.

However, I'd like to do the same thing with the text box. Say, when I hover over the picture of the dog, I want the text in the div near the bottom of the page called "dogdiv" to appear in the text box with the id textarea. I know how to do this with JavaScript (JS) but due to restrictions on the server we're using, that's no longer an option. It has to be straight CSS.

I know it's possible, I just can't seem to find out how. Could any of you tell me what I need to do in order to make this work?

<html> 
<head>
<title>Text/Photo Swap Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
body {background: url('gradient.png');}

#gallery {position: relative; }
#gallery ul {list-style-type: none;
    width: 300px; }
#gallery li { display: inline; 
     float: left;
     padding: 5px; }
#gallery img {border-style: solid: 10px; border-color: #333; }
#gallery a { text-decoration: none;
             font-style: none;  
             color: #333; }

#mainPicture {
    float: right;
        }

.popOut
{
    cursor: default;
    list-style: none;
}
.popOut a
{
    cursor: default;
}
.popOut a .preview
{
    display: none;
}
.popOut a:hover .preview
{
    display: block;
    position: absolute;
    top: -33px;
    left: -45px;
    z-index: 1;
}
.popOut img
{
    background: #ebf0f3;
    border-color: #ebf0f3;
    border-style: solid;
    border-width: 1px;
    color: inherit;
    vertical-align: top;
    width: 100px;
    height: 75px;
}
.popOut li
{
    background: #ebf0f3;
    border-color: #CCCCCC;
    border-style: solid;
    border-width: 0px;
    color: inherit;
    display: inline;
    float: left;
    margin: 3px;
    padding: 2px;
    position: relative;
}
.popOut .preview
{
    border-color: #000;
    width: 200px;
    height: 150px;
}
.popOut p
{
    text-align: center;
}

#textarea
{
    background-color: #ebf0f3;
}

div.hideText
{
display:none;
}


</style>
</head>

<body>
<div class="mainPicture"><img id="awesome" src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/awesome.png" height="300" width="400" alt="" name="Awesome2"><!--style="position:relative; left:600px; top:326px;"--></div>

<td width="1000" rowdiv="3">
<div id="gallery">
    <ul class="popOut">
        <li>
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/dog.jpg';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/dog.jpg';">
                <img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/dog.jpg" width="100" height="75" alt="dog"><img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/dog.jpg" width="100" height="75" alt="dog" class="preview"> 
            </a>
        </li>
        <li>
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/cat.jpg';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/cat.jpg';">
                <img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/cat.jpg" width="100" height="75" alt="cat"><img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/cat.jpg" width="100" height="75" alt="cat" class="preview"> 
            </a>
        </li>
        <li>
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/parrot.jpg';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/parrot.jpg';">
                <img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/parrot.jpg" width="100" height="75" alt="parrot"><img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/parrot.jpg" width="100" height="75" alt="parrot" class="preview"> 
            </a>
        </li>
        <li>
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/lizard.jpg';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/lizard.jpg';">
                <img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/lizard.jpg" width="100" height="75" alt="lizard"><img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/lizard.jpg" width="100" height="75" alt="lizard" class="preview"> 
            </a>
        </li>
        <li>
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/horse.jpg';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/horse.jpg';">
                <img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/horse.jpg" width="100" height="75" alt="horse"><img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/horse.jpg" width="100" height="75" alt="horse" class="preview"> 
            </a>
        </li>
        <li>
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/chicken.jpg';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/chicken.jpg';">
                <img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/chicken.jpg" width="100" height="75" alt="chicken"><img src="http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/chicken.jpg" width="100" height="75" alt="chicken" class="preview"> 
            </a>
        </li>
        <li onMouseOver="textarea.textarea2='chickendiv'">
            <a href="" onMouseOver="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/awesome.png';" onMouseOut="document.Awesome2.src='http://ctd-dev.jdc.ao.dcn/sites/ctd-dev.jdc.ao.dcn/files/pictures/awesome.png';">
                <p>Default Image</p>
            </a>
        </li>
    </ul>

    <div id="textarea" style="height:600px;width:320px;font:14px;font:14px/26px Arial, Helvetica, sans-serif;overflow:scroll; position:relative;
    top:12px;"><div id="textarea2"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi pulvinar lectus non lectus auctor egestas. Donec at nunc neque. Morbi ornare condimentum lobortis. Nam vehicula urna ac mauris pharetra rutrum. Duis et arcu eget sapien interdum porttitor ut et tortor. Maecenas ultricies dignissim pretium. Integer quis enim metus. Sed enim lacus, eleifend eu euismod volutpat, blandit eu sem. Vestibulum varius purus ut est accumsan pellentesque. Donec quis enim id lectus sollicitudin suscipit at volutpat augue. Curabitur et metus purus. Fusce luctus nunc vitae sapien pharetra id accumsan lectus malesuada.</p></div>
    </div>
    </div>
</td>

<div id="dogdiv" class="hideText"><p>The domestic dog (Canis lupus familiaris), is a subspecies of the gray wolf (Canis lupus), a member of the Canidae family of the mammilian order Carnivora. The term domestic dog is generally used for both domesticated and feral varieties. The dog may have been the first animal to be domesticated, and has been the most widely kept working, hunting, and companion animal in human history. The word dog may also mean the male of a canine species, as opposed to the word bitch for the female of the species.</p>
</div>
<div id="catdiv" class="hideText"><p>The domestic cat (Felis catus or Felis silvestris catus) is a small, usually furry, domesticated, carnivorous mammal. It is often called the housecat when kept as an indoor pet, or simply the cat when there is no need to distinguish it from other felids and felines. Cats are valued by humans for companionship and ability to hunt vermin and household pests. They are primarily nocturnal.</p>
</div>
<div id="parrotdiv" class="hideText"><p>Parrots, also known as psittacines are birds of the roughly 372 species in 86 genera that make up the order Psittaciformes, found in most tropical and subtropical regions. The order is subdivided into three superfamilies: the Psittacoidea  parrots), the Cacatuoidea (cockatoos) and the Strigopoidea (New Zealand parrots). Parrots have a generally pantropical distribution with several species inhabiting temperate regions in the Southern Hemisphere as well. The greatest diversity of parrots is found in South America and Australasia.</p>
</div>
<div id="lizarddiv" class="hideText"><p>Lizards are a widespread group of squamate reptiles, with more than 5600 species , ranging across all continents except Antarctica as well as most oceanic island chains. The group, traditionally recognized as the suborder Lacertilia, is defined as all extant members of the Lepidosauria (reptiles with overlapping scales), which are neither sphenodonts (i.e., tuatara) nor snakes ? they form an evolutionary grade. While the snakes are recognized as falling phylogenetically within the Toxicofera clade from which they evolved, the Sphenodonts are the sister group to the Squamates, the larger monophyletic group, which includes both the lizards and the snakes.</p>
</div>
<div id="horsediv" class="hideText"><p>The horse (Equus ferus caballus) is one of two extant subspecies of Equus ferus, or the wild horse. It is an odd-toed ungulate mammal belonging to the taxonomic family Equidae. The horse has evolved over the past 45 to 55 million years from a small multi-toed creature into the large, single-toed animal of today. Humans began to domesticate horses around 4000 BC, and their domestication is believed to have been widespread by 3000 BC. Horses in the subspecies caballus are domesticated, although some domesticated populations live in the wild as feral horses. These feral populations are not true wild horses, as this term is used to describe horses that have never been domesticated, such as the endangered Przewalski Horse, a separate subspecies, and the only remaining true wild horse. There is an extensive, specialized vocabulary used to describe equine-related concepts, covering everything from anatomy to life stages, size, colors, markings, breeds, locomotion, and behavior.</p>
</div>
<div id="chickendiv" class="hideText"><p>The chicken (Gallus gallus domesticus) is a domesticated fowl, a subspecies of the Red Junglefowl. As one of the most common and widespread domestic animals, and with a population of more than 24 billion in 2003, there are more chickens in the world than any other species of bird. Humans keep chickens primarily as a source of food, consuming both their meat and their eggs.</p>
</div>
</body>
</html>

Upvotes: 0

Views: 791

Answers (2)

web_bod
web_bod

Reputation: 5758

stick this in your document ready function

$("img").bind("mouseover", function(){
   var key = $(this).attr("alt");
   $("#textarea").text($("#" + key + "div").text());
}).bind("mouseout",function(){
   $("#textarea").text("");
});

This will attach the event handlers to every single image on the page: $("#popOut>img") would only tag the images in the popout list.

Ok, so to work around the additional constraint of having to do it badly:

JQuery:

<img … 
    onmouseover='$("#textarea").text($("#" + $(this).attr("alt") + "div").text());'
    onmouseout='$("#textarea").text("");'
>

DOM:

<img … 
    onmouseover='document.getElementById("textarea").innerText = document.getElementById( this.alt + "div").innerText;'
    onmouseout='document.getElementById("textarea").innerText = "";'
>

Upvotes: 1

Matt
Matt

Reputation: 2611

This is what you need

<ul id="thumbs">

  <li>
    <div class="large-thumb"><img src="large-thumb.jpg" /></div>
    <div class="small-thumb"><img src="small-thumb.jpg /></div>
  </li>

  <li>
    <div class="large-thumb"><img src="large-thumb.jpg" /></div>
    <div class="small-thumb"><img src="small-thumb.jpg /></div>
  </li>

</ul>

using CSS you would want to apply display: none; to the .large-thumb class see the css below:

#thumbs {
  float: left;
  width: 100%;
  padding: 20px 0;}

#thumbs li {
  float: left;
  display: inline;
  margin-right: 10px;}

#thumbs li .large-thumb { display: none; /*Hide the large thumb*/}

#thumbs li:hover .large-thumb { display: block; /*Show on Hover*/}

I will leave the remaining CSS for you to do but you will need to position the li relatively and then position the large thumbs absolutely to achieve what you're trying to do.

Hope this helps :-)

Upvotes: 1

Related Questions