如何使用jquery翻转动态添加的图像?

时间:2017-09-21 03:13:32

标签: javascript jquery html css

html的

<tbody id="item-determination" class="item-table">

</tbody>

的.js

$('<td><img src="image/' + item['Group 1'].toLowerCase() + '.png"/></td>').appendTo(tr);

根据来自服务程序的数据更新图像并将其附加到表中。 将鼠标悬停在图像上时,我希望它翻转并显示一些文字,就像它在这里发生的那样:https://www.ostraining.com/images/coding/jquery-flip/demo/

但由于我没有使用类似的html结构,如何从我使用的JQuery行中获得相同的效果?

任何形式的帮助将不胜感激。感谢。

$(function () {
        //var $btnAdd = $('#btn-add-item');
      //var $rbFlipType = $('input[name=flipType]');
      var $tblItems = $('#tbl-items');
      var $tbodyItems = $tblItems.children('tbody');

      $(function () {
        //var flipType = $rbFlipType.filter(':checked').val();
        //var random = Math.random() * 9999999999999999;
        var item = `<tr>
            <td>
            <div class="flippable-container">
                <div class="flippable">
                <img src="image/' + item['Group 1'].toLowerCase() + '.png" title=""/>
                <h1>$('<td></td>').text(item['Group 1']).appendTo(tr)</h1>
              </div>
            </div>
          </td>
        </tr>`;

        $tbodyItems.append(item);
      });
    });
</script>

1 个答案:

答案 0 :(得分:0)

你可以通过CSS进行翻转动画,但你真的必须重新调整HTML格式。

&#13;
&#13;
$(function () {
  var $btnAdd = $('#btn-add-item');
  var $rbFlipType = $('input[name=flipType]');
  var $tblItems = $('#tbl-items');
  var $tbodyItems = $tblItems.children('tbody');
  
  $btnAdd.click(function () {
    var flipType = $rbFlipType.filter(':checked').val();
    var random = Math.random() * 9999999999999999;
    /* Template literal
    var item = `<tr>
    	<td>
      	<div class="flippable-container">
        	<div class="flippable ${flipType}">
          	<img src="http://lorempixel.com/400/200/cats?randomQueryString=${random}" title="">
            <h1>${flipType}</h1>
          </div>
        </div>
      </td>
    </tr>`;
    */
    var item = '<tr>' +
      '<td>' +
        '<div class="flippable-container">' +
          '<div class="flippable ' + flipType + '">' +
            '<img src="http://lorempixel.com/400/200/cats?randomQueryString=' + random + '" title="">' +
            '<h1>' + flipType + '</h1>' +
          '</div>' +
        '</div>' +
      '</td>' +
    '</tr>';
    
    $tbodyItems.append(item);
  });
});
&#13;
* {
  margin: 0;
  padding: 0;
}

html, body {
  font: normal 14px/1.8 Helvetica, Arial, sans-serif;
}

.flippable-container {
  perspective: 1000px;
}

.flippable {
  height: 200px;
  position: relative;
  transform-style: preserve-3d;
  transition: 0.6s;
  width: 400px;
}

.flippable img,
.flippable h1 {
  backface-visibility: hidden; /* Don't show the backside of the element when rotated. */
  height: 200px;
  left: 0;
  position: absolute;
  top: 0;
  width: 400px;
}

.flippable img {
  z-index: 1; /* Keep the img in front. */
}

.flippable h1 {
  background: #eee;
  line-height: 200px;
  text-align: center;
}

.flippable.vertical img {
  transform: rotateX(0);
}

.flippable.vertical h1,
.flippable.vertical:hover {
  transform: rotateX(180deg);
}

.flippable.horizontal img {
  transform: rotateY(0);
}

.flippable.horizontal h1,
.flippable.horizontal:hover {
  transform: rotateY(180deg);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <label for="rb-flip-type-horizontal">
    <input id="rb-flip-type-horizontal" type="radio" value="horizontal" name="flipType">
    Horizontal
  </label>
  <label for="rb-flip-type-vertical">
    <input id="rb-flip-type-vertical" type="radio" value="vertical" name="flipType">
    Vertical
  </label>
  <button id="btn-add-item">
    Add
  </button>
  <table id="tbl-items">
    <tbody></tbody>
  </table>
</div>
&#13;
&#13;
&#13;

基于David Walsh's flip