css - How to animate the icon of a jQuery Mobile collapsible heading? -


trying use standard icons of jquery mobile nice rotation animation when collapsible collapsed or expanded, i'm getting strange result, whole collapsible title in heading rotated.

in ideal solution, use predefined jqm icon classes, without need add additional style purpose.

example: icon-carat-u , icon-carat-d rotating 180 degrees animating when collapsible expanding , collapsing, respectively.

moreover, try avoid use click event, because collapsible icon should animate when using in code collapsible("expand") or collapsible("collapse").

here code:

.ui-icon-carat-d {      transform: rotate(-180deg);       transition: .3s;  }  .ui-icon-carat-u {      transform: rotate(0deg);      transition: .3s;  }
<!doctype html>  <html>  <head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  </head>    <body>    <div data-role="page" id="page-one">      <div data-role="content">        <div data-role="collapsible" data-collapsed-icon="carat-d" data-expanded-icon="carat-u">          <h4>heading</h4>          <p>i'm collapsible content. default i'm closed, can click header open me.</p>        </div>      </div>    </div>  </body>    </html>

how can rotate standard carat icons nice smooth animation in jquery mobile collapsible heading?

first thing's first - reason entire bar spinning in code rather caret, .ui-icon-carat-d , .ui-icon-carat-u classes on bar itself. caret icon in ::after pseudo-class of bar.

with said, i'm going preface saying not beautiful solution, solution nonetheless.

to understand why can't more simple, have realize what's happening in jquery mobile's stylesheet caret. essentially, caret , upside-down caret separate, unrelated svg icons being swapped out css-encoded background-images.

because of this, change cannot animated as-is - browser doesn't know images, , can't clever animation between them.

so, solution begins copying/pasting svg background-image represents "regular" caret jquery mobile's styles, , forcing show upside-down state, can animate in traditional way.

as such, i'm targeting element caret icon (an ::after pseudo-class), applying regular caret svg background-image it, , forcing hold on image both states of toggle (with !important).

then, when jquery mobile adds .ui-icon-carat-u class (upside-down), image no longer changes, , rotate icon 180 degrees transform, , animate transition.

hope helps! test below.

.ui-collapsible-heading-toggle::after {    background-image: url("data:image/svg+xml;charset=us-ascii,%3c%3fxml%20version%3d%221.0%22%20encoding%3d%22iso-8859-1%22%3f%3e%3c!doctype%20svg%20public%20%22-%2f%2fw3c%2f%2fdtd%20svg%201.1%2f%2fen%22%20%22http%3a%2f%2fwww.w3.org%2fgraphics%2fsvg%2f1.1%2fdtd%2fsvg11.dtd%22%3e%3csvg%20version%3d%221.1%22%20id%3d%22layer_1%22%20xmlns%3d%22http%3a%2f%2fwww.w3.org%2f2000%2fsvg%22%20xmlns%3axlink%3d%22http%3a%2f%2fwww.w3.org%2f1999%2fxlink%22%20x%3d%220px%22%20y%3d%220px%22%20%20width%3d%2214px%22%20height%3d%2214px%22%20viewbox%3d%220%200%2014%2014%22%20style%3d%22enable-background%3anew%200%200%2014%2014%3b%22%20xml%3aspace%3d%22preserve%22%3e%3cpolygon%20style%3d%22fill%3a%23ffffff%3b%22%20points%3d%2211.949%2c3.404%207%2c8.354%202.05%2c3.404%20-0.071%2c5.525%207%2c12.596%2014.07%2c5.525%20%22%2f%3e%3c%2fsvg%3e") !important;    transition: transform .3s;  }    .ui-icon-carat-u::after {    transform: rotate(-90deg);  }
<!doctype html>  <html>    <head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  </head>    <body>    <div data-role="page" id="page-one">      <div data-role="content">        <div data-role="collapsible" data-collapsed-icon="carat-d" data-expanded-icon="carat-u">          <h4>heading</h4>          <p>i'm collapsible content. default i'm closed, can click header open me.</p>        </div>      </div>    </div>  </body>    </html>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -