最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do I swap triangle icons when doing expandingcollapsing divs with JavascriptCSS - Stack Overflow

programmeradmin1浏览0评论

I have a div that I want to toggle between displaying or not when a user clicks on a piece of text. I have a javascript function that toggles a div on or off. That works fine. What I don't know how to do is to have a triangle that points to the right or down depending on whether the div is open.

My HTML looks like this:

<a onclick="toggleDiv('myDiv');">Click here to expand or close div</a>
<div id="myDiv" style="display:none">
   ...
</div>

How do I add one of those triangles and have it point the right way, ideally with just HTML, Javascript, and CSS? The triangle would appear to the left of the "Click here..." string.

Thanks.

I have a div that I want to toggle between displaying or not when a user clicks on a piece of text. I have a javascript function that toggles a div on or off. That works fine. What I don't know how to do is to have a triangle that points to the right or down depending on whether the div is open.

My HTML looks like this:

<a onclick="toggleDiv('myDiv');">Click here to expand or close div</a>
<div id="myDiv" style="display:none">
   ...
</div>

How do I add one of those triangles and have it point the right way, ideally with just HTML, Javascript, and CSS? The triangle would appear to the left of the "Click here..." string.

Thanks.

Share Improve this question asked Feb 28, 2010 at 18:48 SL.SL. 531 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Your toggleDiv() function would be helpful to see here. Anyhow you should probably just add a class to your div, perhaps "active" or "open".

Then in your CSS, do something like:

div {
  background: url("downarrow.png") top left no-repeat; 
}

div.open {
  background: url("uparrow.png") top left no-repeat; 
}

update

You also may consider moving your JS out of your HTML entirely, you can observer the click event on the element from your JS, then add the class or remove it based on it's state. Maybe consider using a library like jQuery.

There are a number of options. First you will need the two images, let us call them uptri.jpg and downtri.jpg. Then you can create two CSS classes:

   .up{
       background-image:url(../images/uptri.jpg);
       background-repeat:no-repeat;
   }

   .down{
       background-image:url(../images/downtri.jpg);
       background-repeat:no-repeat;
   }

Then with some JavScript you can swap out the up class for the down class. I would suggest using jQuery since it makes it trivially easy.

   $("#myClicker").toggle(
         function(){$(this).removeClass('up');
                     $(this).addClass('down');
         }, 
         function(){$(this).removeClass('down');
                     $(this).addClass('up');
         }     
    );

Where myClicker is the id of your link.

You can also have an image sprite, which is a single image that has both of the arrows, preventing the need for fetching two images. Use CSS to change the position of the background image so that only one shows at a time. For example, here is one state:

#triangle {
    background-image: url(triangle.png);
    background-repeat: no-repeat;
    background-position: 0 0;
}

then use javascript to add a CSS class or directly manipulate to:

    background-position: 15px 0;

or similar to shift the next triangle into place.

发布评论

评论列表(0)

  1. 暂无评论