Please note: This plug-in concentrates on drawing the SVG icon, which may even
be animated with CSS transitions. The actual content-folding is not part of this plug-in.
The following examples add some very simple folding with jQuery's slideDown()
and slideUp()
methods with a delay argument. These slideDown() and slideUp()
mechanism actually needed some tricky CSS tweaks to really run smoothly (only applied
in the »Foldable Sections« examples below), please refer to the source code of this page
if interested.
Let's start the examples with the most common use-case: foldable sub-lists in list items.
For this example, we create a demo HTML list with the following markup:
<ul id="l1" class="folding-arrows">
<li id="g1"><a href="#!">Test list item</a>
<br>
Second line
<ul>
<li>sub</li>
<li>list</li>
</ul>
</li>
<li id="g1b" class="showing">
<a href="#!">Test list item</a>
<br>
Second line
<ul class="folding-arrows">
<li>sub<br>still sub</li>
<li>list</li>
</ul>
</li>
</ul>
This is what it looks like – with the folding arrow icon plugin applied with default settings:
The examples above demonstrate the default shape of the folding arrow icon, being a closed triangle pointing to the right resp. down. The javascript to include them is quite simple:
$("#l1 > li, #l1 ul.folding-arrows > li").prependFoldingArrowIcon();
This JavaScript alone is not enough, since the inserted SVG is unstyled, so you need to add some CSS code to define the size and color theme:
This Library already comes with an included CSS file (jquery-folding-arrow-icon.css
),
which defines a default size for the images and the transition for rotating the arrow when
the CSS class of the list item changes. Specifically for icons in unordered lists,
it also contains rules which align list items without bullets and with this SVG image
(inline inside the li's content) neatly with other normally bulleted list items
(see first sub list with default list bullets and compare its alignment to the second
sub list equipped with arrow icons).
But including that CSS file still isn't enough, since it does not define the actual look (colors and line with) of the graphic. That, you' still have to define on your own (otherwise it will default to a black triangle). This demo actually defines two different color settings for the first and the second list item:
#g1 .folding-arrow-icon path {
stroke: none;
fill: silver;
}
#g1b .folding-arrow-icon path {
stroke: none;
fill: #a00
}
Now, you just have to add the class "showing" to a list item if its icon should be transformed to a down-pointing arrow, removing the class transforms it back into a right-pointing arrow. Actually, in the example above, the second list item has been fitted with a "showing" class statically (see HTML markup shown above), which is why it's already unfolded upon page load, while the first is initially folded.
Please note that this jQuery plug-in does not hide or show the actual
sub lists, but concentrates on just providing the animatable triangle icon. The
actual folding has to bee implemented separately (though I'm planning to release
a higher-level jQuery plug-in in the future, which will simplify showing or hiding sections
and adding a class like showing
to a switch element in order
to display the folding state like – but not limited to – using this icon plug-in).
This example page uses the following JavaScript code (for the demo above as well as the following ones) in order to show and hide sub-lists:
$(function() {
function openOrClose(jqr) {
return jqr.toggleClass("showing").transformFoldingArrowIcon();
}
function openOrCloseLi(li) {
openOrClose(li);
if (li.is(".showing"))
$("ul", li).slideDown(500);
else
$("ul", li).slideUp(500);
}
$("ul.folding-arrows > li > a, ul.folding-arrows > li > svg.folding-arrow-icon:not(.static)").click(function(){
ev.preventDefault();
openOrCloseLi($(this).parent());
});
$("ul.folding-arrows > li:not(.showing) > ul").hide();
});
Please note the calling of a further jQuery plug-in function provided by this library:
transformFoldingArrowIcon()
. As long as your page is viewed with a browser
that supports transformations via CSS, this plug-in is not needed, since the loaded
default CSS already defines the rotation transformation along with a CSS transition.
The click handler is only applied to those SVGs of class folding-arrow-icon
which do not also have the class static
. The latter is used by default to
denote icons for static, not foldable list items. The shape of these icons might
be a dash or disc (see examples for dash or disc presets further below) or some
custom-designe other icons like a diamond, square etc.
Unfortunately, the Microsoft browsers (Edge and Internet Explorer) do not support
CSS transformations for inline SVG, they require the transformations defined in
tag attributes inside the SVG code. The transformFoldingArrowIcon()
function adds or removes such a transformation attribute depending on the current
state (whether the element has or has not the class "showing"). If called without
parameters, the default transformation is used.
Please note: If both a transition attribute and a CSS transition are defined for the same SVG element, the CSS transition is used and the attribute is ignored.
Click a variation list item in order to view the description and code samples.
#outline .folding-arrow-icon path {
stroke: navy;
stroke-width: 1;
fill: none;
}
viewboxMargin
, which defaults to 1,
is overridden to be 2. This increases the margin around the image inside
the SVG area a little, because due to the stroke with the triangle icon
would otherwise be slightly larger than the filled one without stroke, and
during the rotation transition, the edges of the triangle would exceed
the visible image area, i.e. be clipped:
$("#outline > li").prependFoldingArrowIcon({
viewboxMargin: 2
});
viewportMargin
, resulting
in a slightly larger graphic and accepting a slight clipping during the
rotation transition, which hardly is noticable in this case (with the closed
triangle, it would be easier to notice).closePath
option to false.
The triangle arrow is in fact acutally defined as a the chevron path you
see here, but the option closePath
, which defaults to true,
will cause this main path to be closed. This default behaviour is now turned
off, resulting in the “raw chevron path” to be drawn.
$("#chevron > li").prependFoldingArrowIcon({
closePath: false
});
#chevron .folding-arrow-icon path {
stroke: navy;
stroke-width: 1;
fill: none;
stroke-linecap: round;
}
$("#custom-arrow > li").prependFoldingArrowIcon({
graph: [{element: "path", attributes: {"d": "M0,-9 L10,0 L0,10 M-10,0 L8,0"}}],
viewboxRadius: 10,
viewboxMargin: 3,
closePath: false
});
graph
defines the main SVG code. You define an array of SVG elements.
Each element of the array is an object with two properties: element
defines the element/tag name of the SVG element, and attributes
is an object ob key/value pairs defining a list of attributes to insert into
that tag.graph
array above thus specifies the following SVG tag:
<path d="M0,-9 L10,0 L0,10 M-10,0 L8,0"/>
graph
are internally grouped into one
group element (g
).viewboxRadius
option defines the radius of the viewport,
i.e. the visible part of the coordinate system. Acutally the viewport is
a square, in this case (radius 10) the point (-10, -10) is the upper left corner
and (10, 10) the lower right corner.viewboxRadius
defines the size of the square area
into which the graph
shoult fit, the option viewboxMargin
defines the width of a margin around that graph.viewboxMargin
should therefore
be at least half the stroke-width!viewboxRadius
and viewboxMargin
are simply
added to define the top left corner and the size of the viewport.
The separation into two options instead of one
is simply for better readability/maintainability of the options.closePath
option has already been introduced in an
earlier example. If set to true, a z
will be appended to
every path attribute of the graph, which will add a connecting line
between the start point and the end point of the path. In this case,
that's not desirable, so it's turned off.#custom-arrow .folding-arrow-icon path {
stroke: green;
stroke-width: 2;
fill: none;
}
graph
array: a circle around the center coordinate (0, 0)
with a radius of 20.viewportRadius
is set to the radius of the circle (20).viewboxMargin
should be at least half the stroke width, since
half of the stroke width is applied inside the logical line of the circle, the
other half outside (in the margin area). So we set it to 2.$("#custom-arrow-circle > li").prependFoldingArrowIcon({
graph: [{element: "circle", attributes: {"cx": "0", "cy": "0", "r":"20"}},
{element: "path", attributes:{"d": "M0,-9 L10,0 L0,10 M-10,0 L8,0"}}],
viewboxRadius: 20,
viewboxMargin: 2,
closePath: false
});
#custom-arrow-circle .folding-arrow-icon path, circle {
stroke: green;
stroke-width: 3;
fill: none;
}
graph
option
to define the SVG image, this time consisting of two lines forming a plus.
The viewboxRadius
is set to the width resp. height of the horizontal resp.
vertical line.
jquery-folding-arrow-icon.css
defines
the transformation and a transition (animation) by the following rules:
svg.folding-arrow-icon > g {
transition: transform 0.5s;
}
.folding-arrow.showing > svg.folding-arrow-icon > g {
transform: rotate(90deg);
}
transformFoldingArrowIcon()
plug-in function, see the
function openOrClose(jqr)
defined above. This
function adds or removes transformation attributes to resp. from
SVG node(s). The default action is to add a 90deg right rotation
transformation to the g
node, the group holding
the entire graph defined by the graph
option.
svgClass
option to assign a
second class name (my-plus
) to the svg
element.
Yet, by also keeping the default class name (folding-arrow-icon
),
the default rules still apply. my-plus
class, setting the
transition to 45 degrees.—Actually, to set this example apart from
the preset shown in the next example, let's vary the angle to 135°.
The result will look the same, but the transition (animation) looks
different, since the plus rotates further and faster.
transformFoldingArrowIcon()
plug-in function,
we add a setupFoldingArrowIconTransformation()
call to
the prependFoldingArrowIcon()
call. Here we pass two
options:
svgClass
holds class name used for finding the CSS node inside the
list item on which the tronsfromFoldingArrowIcon()
function
is called. This should normally math the svgClass
option passed
to prependFoldingArrowIcon()
.transformations
holds an array of key-value-pairs,
each of which defines a node selector as key and a the value for the
transformation
attribute which should be added to the
selected node upon unfolding.g
node in the CSS DOM, i.e. the group consisting
of the two crossed lines.
$("#plus-manual > li").prependFoldingArrowIcon({
graph: [{element: "line", attributes: {"x1": "-10", "y1": "0", "x2": "10", "y2":"0"}},
{element: "line", attributes: {"x1": "0", "y1": "-10", "x2": "0", "y2":"10"}}],
svgClass: "folding-arrow-icon my-plus",
viewboxRadius: 10,
viewboxMargin: 1
}).setupFoldingArrowIconTransformation({
svgClass: "folding-arrow-icon my-plus",
transformations: [{">g": "rotate(45)"}]
});
.folding-arrow.showing > svg.folding-arrow-icon.my-plus > g {
transform: rotate(135deg);
}
.my-plus line {
stroke: navy;
stroke-width: 2;
}
preset
option with the preset name (as string value).setupFoldingArrowIconTransformation()
plug-in function with the
preset defining the transformation (usually simply the same preset name, except
if you want to combine a graphic from one preset with a transformation from another).setupFoldingArrowIconTransformation
won't
seem to do any bad – as long as you use a browser supporting CSS transformations for SVG.
(The default transformation and transition for this preset are defined in the bundled
file jquery-folding-arrow-icon.css
.) But if the page is opened in a
Microsoft browser, which doesn't yet support CSS transformations, the inline transformations
defined by this plug-in function will become “alive”. And since the default transition
is a rotation by 90°, this won't have any visible effect on the plus icon!g
node!$("#plus-preset > li").prependFoldingArrowIcon({
preset: "plus"
}).setupFoldingArrowIconTransformation({
preset: "plus"
});
#plus-preset .plus line {
stroke: #555;
stroke-width: 3;
}
$("#plus-preset > li").prependFoldingArrowIcon({
preset: "plus-minus"
}).setupFoldingArrowIconTransformation({
preset: "plus-minus"
});
#plus-minus-preset .plus-minus line {
stroke: #555;
stroke-width: 3;
}
$.fn.prependFoldingArrowIcon.copyOfPreset()
(not a jQuery plug-in,
but a static helper function published in the plug-ins namespace) takes the name of a preset
and returns an object of a special PresetCopy class. This object can always be used as
argument to the preset
option instead of a preset name (string), but before that,
you may apply modifications to that preset-copy using its methods.prependToGraph()
and appendToGraph()
.
Remember that the graph is an array of SVG element definitions, and these methods may
define further SVG elements which are added to the graph array at its beginning resp. end.
They take two arguments: first the element name (tag name), second an object of key-value-pairs
defining the attributes to add to that tag.viewboxRadius
also has to
be enlarged (to 17), and in order to leave some room for the stroke of width 4, we have to
define a viewboxMargin
of at least 2. For some more tolerance, we choose 3.preset
option with "hard-coded" other options,
the latter then override the preset's options!setupFoldingArrowIconTransformations()
call we simply use
the original preset, since our copy of the preset only overrides the preset's graph,
the inherited transformation properties haven't changed and are also applicable on the
changed graph (since the added circle does not have to be transformed).$("#plus-circle > li").prependFoldingArrowIcon({
preset: $.fn.prependFoldingArrowIcon.copyOfPreset("plus")
.prependToGraph("circle", {"cx": "0", "cy": "0", "r": "17"}),
viewboxRadius: 17,
viewboxMargin: 3
}).setupFoldingArrowIconTransformation({
preset: "plus"
});
ul.plus-circle .plus line, ul.plus-circle .plus circle {
stroke: #555;
stroke-width: 4;
fill: none;
}
var myPlusMinusCirclePreset = $.fn.prependFoldingArrowIcon.copyOfPreset("plus-minus")
.prependToGraph("circle", {"cx": "0", "cy": "0", "r": "17"})
.prop("viewboxRadius", 17)
.prop("viewboxMargin", 3);
$("#plus-minus-circle > li").prependFoldingArrowIcon({
preset: myPlusMinusCirclePreset
}).setupFoldingArrowIconTransformation({
preset: myPlusMinusCirclePreset
});
dash
and non-folding list items
graph
option.)$("#bullets li.arrow.default").prependFoldingArrowIcon().setupFoldingArrowIconTransformation();
$("#bullets li.dash, #bullets ul.dashes li").prependFoldingArrowIcon({preset: "dash"});
The first line prepends the default arrow icon and its default fallback
transformation as demonstrated above already. The second line applies
the dash
preset to the next list item (li
of class dash
)
and to every list item of this sub list (ul
of class dashes
— and of class folding-arrows
in order to inherit the styles
of the included CSS file which, among others, turn off the default list bullets).
Since these are static list items, no transformation is defined.
default
denotes these first silver examples, while the later,
examples using thin blue strokes are denoted by class chevron
):
#bullets .default svg path,
#bullets .default svg circle {
stroke: none;
fill: silver;
}
#bullets .default svg.dash line {
stroke: silver;
stroke-width: 2px;
}
The first rule defines the look of the arrow icon and the disc bullet in an example further down, the second rule defines the look of the dash icon if the next list item.
dash
preset adds the classes static
and dash
to the SVGs class attribute. The latter can be used for styling (even if this has not been
done in this example, since we used the class of the list item itself) and the first one
is meant to denote a general un-foldable list item. This has
been used in the code presented in the introduction to this demo page which applies
a click handler to all folding-arrow-icons except those of the
static
class (see above)disc
and non-folding list items
disc
instead of dash
:
$("#bullets li.disc").prependFoldingArrowIcon({preset: "disc"});
dash
and non-folding list items – alternative style
path
,
the second selector selects the line of the dash icon, the third selects
the circle of the disc item. In this example, the actual CSS style rules are
the same for all three shapes:
#bullets .chevron svg path,
#bullets .chevron svg line,
#bullets .chevron svg circle {
stroke: navy;
stroke.width: 1px;
fill: none;
}
The plug-in isn't limited to creating list item bullets. To show just one alternative,
the following examples will all use a heading (h2
) to show or collapse
a document section by clicking the heading.
The secions in these examples all have the following structure:
<h2 class="sec"><a href="#!">This is a section header</a></h2>
<div>
<p>Section content. Note this is all wrapped in one single div in order to simplify
collapsing or re-showing this content block.</p>
<p>See JavaScript function openOrCloseSection() below, which will simply hide or show
the first sibling after the heading, which is exactly this div.</p>
</div>
The whole heading should accept a mouse click event, but the link element (a
)
inside is to make sure that the section can also be toggled by keyboard users (accessibility!).
See the registerClickHandler()
function below, which registers one click
handler at the heading itself, one on the link, and the latter has to prevent event bubbling
so that the handler is not executed twice (once for clicking the link and a second time for
clicking the heading)!
All of the section examples use some common JavaScript and CSS code.
Note that the openOrCloseSection()
function not only folds or unfolds
one section, but also any other currently open section will be folded prior to
unfolding a newly selected one. Of course this is only an example, but this behaviour
is often found on the web.
function openOrClose(jqr) {
return jqr.toggleClass("showing").transformFoldingArrowIcon();
}
function openOrCloseSection(h) {
if (!h.is(".showing")) {
//collapse currently open section(s):
$("h2.sec.showing").each(function() {
openOrCloseSection($(this));
});
openOrClose(h);
h.next().slideDown500);
} else {
openOrClose(h);
h.next().slideUp(500);
}
}
function registerSectionClickHandler(section) {
section.click(function() {
openOrCloseSection(section)
}).children("a").first().click(function(ev) {
ev.preventDefault();
ev.stopPropagation();
openOrCloseSection(section);
});
}
$(function(){
$("h2.sec").each(function() {
//Initially collapse all sections.
//Do this dynamically by script in order to have all section opened and visible
//in case JavaScript should be disabled in the browser.
$(this).next().hide();
}).setupFoldingArrowIconTransformation({
//Global title common to all of the examples
titleHidden: "open section"
});
})
h2.sec {
border: 1px solid silver;
background-color: #eee;
border-radius: 0.3em;
padding: 0.2em;
margin-top: 0;
}
h2.folding-arrow + div > :first-child {
margin-top: 0;
}
h2.folding-arrow + div > :last-child {
margin-bottom: 0;
}
h2.folding-arrow + div {
margin-bottom: 2em;
}
/*
Note: the margins above (no top margin of the h2 itself and no top margin
for the first child in the hideable content resp no bottom margin for
the content's last child are chosen in order to enable a smooth folding animation
by jQuery's slideDown) and slideUp() methods. Margins in these places would cause "jumps"
at the start and end of the animation, as the top margin of an element gets usually
"merged" with the bottom margin of its next sibling, while during the animation they
add up.
*/
This is a very simple example and uses the same function to prepend an icon as did the previous list item demos:
registerSectionClickHandler(
$("#sec-prefix").prependFoldingArrowIcon({
closePath: false
}).setupFoldingArrowIconTransformation()
);
As with the list item examples, you still have to add CSS styles to define the looks of the generated SVG. Here, we'll define some styles which will not only be used for this example, but also for some more of the following demos:
h2.folding-arrow .folding-arrow-icon path {
stroke: navy;
stroke-width: 2;
fill: none;
}
A variation of the previous example, differing in the following aspects:
appendFoldingArrow()
instead of
prependFoldingArrow()
, which, as its name suggests, inserts the
SVG icon at the end of the heading element instead of at its beginning.arrow-up-down
, which shows a down-pointing triangle
for collapsed and an up-pointing triangle for unfolded sections and doesn't use
a rotation transition, but a vertical mirroring transition to switch between them.
(Of course you could override that by defining a 180° rotation, too.)registerSectionClickHandler(
$("#sec-postfix").appendFoldingArrowIcon({
preset: "arrow-up-down",
closePath: false
}).setupFoldingArrowIconTransformation({
preset: "arrow-up-down"
})
);
No further CSS code needed: This example re-uses the CSS shown in the previous example
for defining the looks of the arrow icon itself and the included
jquery-folding-arrow-icon.css
file's default rules for the transition
defined by the preset.
Again, a variation of the previous example, mainly differing in the alignment of the icon via special CSS code. The following CSS is used for this and all other following section examples:
h2.sec.al-right .folding-arrow-icon {
float: right;
width: 1em;
height: 1em;
margin-top: 0.1em;
}
The JavaScript code is mainly the same, only one variation has been made:
Since the Icon is independently aligned, the separator
property
gets redefined in order not to append an unnecessary space character to the
heading text (even though that would not actually be visible):
registerSectionClickHandler(
$("#sec-al-right").appendFoldingArrowIcon({
preset: "arrow-up-down",
closePath: false,
separator: ""
}).setupFoldingArrowIconTransformation({
preset: "arrow-up-down"
})
);
This example is a variation of the previous one, simply adding a circle to the "arrow-up-down" preset's icon.
In this case, the circle should be filled and should be prepended to the arrow icon, i.e. be placed behind the latter. Since the background circle is filled with a dark (blue) color, the foreground icon is formatted uses a white stroke.
For more explanations on the code, please refer to the previous list item examples!
var arrowUpDownInCircle = $.fn.prependFoldingArrowIcon.copyOfPreset("arrow-up-down")
.prependToGraph("circle", {"cx": "0", "cy": "0", "r": "10"})
.prop("viewboxRadius", 10)
.prop("viewboxMargin", 0)
.prop("closePath", false);
$("#sec-al-right-circle1, #sec-al-right-circle2").prependFoldingArrowIcon({
preset: arrowUpDownInCircle,
separator: ""
})
registerSectionClickHandler(
$("#sec-al-right-circle1").setupFoldingArrowIconTransformation({
preset: arrowUpDownInCircle
})
);
#sec-al-right-circle1 .folding-arrow-icon circle {
stroke: none;
fill: #007;
}
#sec-al-right-circle1 .folding-arrow-icon path {
stroke: white;
}
In the previous example, the whole graphic, i.e. the circle with the white chevron in it, gets flipped vertically. In the end result, only the arrow has changed its direction, while the circle still looks the same (naturally). Yet, during the transition, the whole circle gets shrunken and expanded again. This may feel a litte obtrusive, so this example provides a more discreet transition.
It simply differs from the previous example only in the one aspect, that instead of the whole icon (or, to be more precise, the group inside the SVG image which consists of two shapes, the circle and the array) now only the arrow shape is to be flipped, while the circle rests untouched.
The main Javascript code and CSS styles are exactly the same as in the example above (except, of course, that a different section ID is selected). Here, we'll only show the code defining the different transformation and transition:
#sec-al-right-circle2 .folding-arrow-icon > g {
transition: none;
transform: none;
}
#sec-al-right-circle2 .folding-arrow-icon path {
transition: transform 0.5s;
}
#sec-al-right-circle2.showing .folding-arrow-icon path {
transform: scale(1, -1);
}
The first CSS rule overrides the default transition and tranformation for the group (g
)
inherited from the included CSS file. The other two rules define a transition for the path node
and a transformation for the path node in case it is marked as showing
.
Keep in mind, the image inside the generated SVG contains one group element g
with two childen: a circle
and a path
element,
and we only want to transform the latter.
Since the end result looks the same as in the previous example, only the transitions (animations)
differ, and the IE-/Edge fallback defined by setupFoldungArrowIconTransformation
does not support transition, one might think at first glance, that nothing has to be changed
here, the setup method could still be called with the same preset as in the previous example.
But that assumption would be wrong! The reason for that is quite simply, that, if
the CSS rules would transform the arrow icon and the attribute rules would transform the whole
group, both transformations would be applied simultaniously, effectively cancelling
each other out (at least in browsers supporting CSS transformations). Attribute and CSS
transformations can only safely co-exist, if they define transformations for the very same
SVG elements, in which case (as already explained in »Foldable List Items«), the CSS transition
overrides the attribute transition.
So, we have to redefine the transformation attribute accordingly:
registerSectionClickHandler(
$("#sec-al-right-circle2").setupFoldingArrowIconTransformation({
transformations: [{"path": "scale(1 -1)"}]
})
);
This example picks up the plus preset again, which has already been demonstrated in the list items section, but was actually mainly created to be used this way in a section header.
Here, we don't address a single header by its ID, but we use a class selection for all
headers of classes folding-plus
and class x
:
$("h2.folding-plus.x").prependFoldingArrowIcon({
preset: "plus",
viewboxMargin: 3,
separator: ""
}).setupFoldingArrowIconTransformation({
preset: "plus"
})
registerSectionClickHandler($("h2.folding-plus.x:not(.altered)"));
By constraining the selector to the additional class x
this script setup is specific
to this demo with the plus, which should be rotated by 45° to form an X when the section
is unfolded.
The following CSS, however, is not constrained to the x
class, but is
shared with the following demos using a plus, which gets transformed into a minus.
h2.sec.folding-plus a:link, h2.sec.folding-plus a:visited {
color: #555;
}
h2.sec.folding-plus:hover a, h2.sec.folding-plus a:focus {
color: #33e;
}
h2.sec.folding-plus .folding-arrow-icon line {
stroke: #555;
stroke-width: 3;
stroke-linecap: round;
}
h2.sec.folding-plus:hover .folding-arrow-icon line {
stroke: #33e;
}
To demonstrate a further advantage of inline SVG images over external files loaded
as img
, this demo not only defines a static style for the plus icon,
but it adds a hover effect to the whole header, highlighting the text as well
as the SVG icon when the mouse cursor hovers over the heading.
This example only differs from the previous one by loading the "plus-minus" preset which defines a different transformation (plus to minus instead of plus to X):
registerSectionClickHandler(
$("h2.folding-plus.minus").prependFoldingArrowIcon({
preset: "plus-minus",
viewboxMargin: 3,
separator: ""
}).setupFoldingArrowIconTransformation({
preset: "plus-minus"
})
);
The CSS code is shared with the previous demo.
This demo is not intended to be used as blueprint for achieving the plus-to-minus transformation, since that's, what the previously demonstrated "plus-minus" preset is for. It's simply a demo of how you might use plug-in options in order to locally modify the transformation defined by a preset.
So let's assume that the "plus-minus" preset were not available and you wanted to simply use the "plus" preset and override its transformation.
In this example wie use the same classes as in the first "plus" demo
and add a further class altered
intended to mark a heading where this
transformation override should be added to:
<h2 class="sec folding-plus al-right x altered" >
.
The "plus"-demo's javascript will now be executed first and thus
will apply the "plus" preset to this heading, too.
But then we execute a second javascript snippet, which will subsequently
alter the transformation setting for this example, defining a different
fallback transition attribute for browsers not supporting CSS transformations:
registerSectionClickHandler(
$("h2.folding-plus.x.altered").setupFoldingArrowIconTransformation({
transformations: [{"line.v": "scale(1 0)"}]
})
);
For browsers with CSS transformations support, the CSS has to be overridden, and here that's not just the transformation itself but also the according transition:
h2.folding-plus.x.altered .folding-arrow-icon.plus > g {
transform: none;
transition: none;
}
h2.folding-plus.x.altered .folding-arrow-icon.plus line.v {
transition: transform 0.7s;
}
h2.folding-plus.x.altered.showing .folding-arrow-icon.plus line.v {
transform: scale(1, 0);
}
Note that in this case (CSS override), we not only add a new transformation
to the vertical line of the plus icon (line
element of class v
as defined by the "plus"
preset), but we also have to reset the
transformation of the g
element, which is usually defined for this preset.
In the JavaScript version above that was not necessary, since overwriting the
transformations
option completely replaces the inherited transformation.
Though this jQuery plug-in is really mainly made for use cases as demoed above, it can be used for slightly different purposes, too, in which a single icon is needed which can be flipped between two states with a simple transition, combinable with a CSS transition.
A burger icon for opening and closing some menu is one such use case, and for this rather
common type if icon, a further preset is included and shall be demoed here. (If it weren't,
of course you could use this plug-in's graph
option to create your own icon,
as already has been demonstrated above.