简体   繁体   English

使用Javascript / CSS扩展/折叠div时如何交换三角形图标

[英]How do I swap triangle icons when doing expanding/collapsing divs with Javascript/CSS

I have a div that I want to toggle between displaying or not when a user clicks on a piece of text. 我有一个div,我想在用户单击一段文本时在显示还是不显示之间切换。 I have a javascript function that toggles a div on or off. 我有一个javascript功能,可以打开或关闭div。 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. 我不知道该怎么办,就是要有一个三角形,该三角形根据div是否打开而指向右边或右边。

My HTML looks like this: 我的HTML看起来像这样:

<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? 如何添加这些三角形之一,并使其正确指向,理想情况下仅使用HTML,Javascript和CSS? The triangle would appear to the left of the "Click here..." string. 三角形将出现在“单击此处...”字符串的左侧。

Thanks. 谢谢。

Your toggleDiv() function would be helpful to see here. 您的toggleDiv()函数将有助于在此处查看。 Anyhow you should probably just add a class to your div , perhaps "active" or "open". 无论如何,您可能应该只向div添加一个类,也许是“活动”或“开放”。

Then in your CSS, do something like: 然后在您的CSS中,执行以下操作:

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. 您还可以考虑将JS完全移出HTML,可以观察JS中元素上的click事件,然后添加类或根据其状态将其删除。 Maybe consider using a library like jQuery. 也许考虑使用类似jQuery的库。

There are a number of options. 有很多选择。 First you will need the two images, let us call them uptri.jpg and downtri.jpg. 首先,您将需要两个图像,让我们将它们称为uptri.jpg和downtri.jpg。 Then you can create two CSS classes: 然后,您可以创建两个CSS类:

   .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. 然后使用一些JavScript,您可以将up类换成down类。 I would suggest using jQuery since it makes it trivially easy. 我建议使用jQuery,因为它非常容易。

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

Where myClicker is the id of your link. 其中,myClicker是链接的ID。

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. 使用CSS更改背景图片的位置,以便一次只显示一个。 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: 然后使用javascript添加CSS类或直接操作以:

    background-position: 15px 0;

or similar to shift the next triangle into place. 或类似的将下一个三角形移到适当位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM