简体   繁体   中英

Jquery.html in each json load not working

I work with this tut: https://www.ostraining.com/images/coding/jquery-flip/demo/ It's work perfectly. This code work good, it show 2 image with affect

<div id="flip">
<div id="flip-this" class="flip-horizontal" style="perspective: 600px; position: relative; transform-style: preserve-3d;">
    <div class="front" style="height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 1; transition: all 0.5s ease-out; transform: rotateY(0deg);">
        <img src="img/source/5.jpg" alt="" style="backface-visibility: hidden;">
    </div>
    <div class="back" style="transform: rotateY(-180deg); height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 0; transition: all 0.5s ease-out;">
        <img src="img/source/5.jpg" alt="" style="backface-visibility: hidden;">
    </div>
</div>
<div id="flip-this" class="flip-horizontal" style="perspective: 600px; position: relative; transform-style: preserve-3d;">
    <div class="front" style="height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 1; transition: all 0.5s ease-out; transform: rotateY(0deg);">
        <img src="img/source/5.jpg" alt="" style="backface-visibility: hidden;">
    </div>
    <div class="back" style="transform: rotateY(-180deg); height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 0; transition: all 0.5s ease-out;">
        <img src="img/source/5.jpg" alt="" style="backface-visibility: hidden;">
    </div>
</div>  
</div>

Because i'm loading data by json, so, i want to load this effect in loop json load, i want it show 10 image in data with affect, my code:

<div id="flip">

</div>
<script>
$(document).ready(function(){
var url="http://localhost/service/load_data.php";
//load 10 image
$.getJSON(url,function(result){
$.each(result, function(i, field){
  var id=field.id;
  var img=field.img;
  $('#flip').append('<div id="flip-this" class="flip-horizontal" style="perspective: 600px; position: relative; transform-style: preserve-3d;"><div class="front" style="height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 1; transition: all 0.5s ease-out; transform: rotateY(0deg);"> <img src="img/source/'+img+'" alt="" style="backface-visibility: hidden;"> </div> <div class="back" style="transform: rotateY(-180deg); height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 0; transition: all 0.5s ease-out;"> <img src="img/source/'+img+'" alt="" style="backface-visibility: hidden;"> </div></div> ');
});
});
});
</script>

It't show image but not work with effect. I think effect run before load json, so it not work. Please help me. Thank you!

You aren't initializing the plugin which you would need to do after you insert the html

Something like

$.getJSON(url, function(result) {
  var $flip = $('#flip')
  $.each(result, function(i, field) {
    var id = field.id;
    var img = field.img;
    $flip.append('<div id="flip....../div> ');
  });
  $flip.flip({ /* plugin options here */ })
});

I know nothing about this specific plugin.

It is possible that it also allows you to add images to an existing instance. If so that would be outlined in the documentation

        <div id="flip-this" class="flip-horizontal" style="perspective: 600px; position: relative; transform-style: preserve-3d;">
    </div> 
    <script>
    $(document).ready(function(){
    var url="http://localhost/service/load_data.php";
    //load 10 image
    $.getJSON(url,function(result){
    $.each(result, function(i, field){
      var id=field.id;
      var img=field.img;
      $('#flip-this').append('<div class="front" style="height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 1; transition: all 0.5s ease-out; transform: rotateY(0deg);"> <img src="img/source/'+img+'" alt="" style="backface-visibility: hidden;"> </div> <div class="back" style="transform: rotateY(-180deg); height: 100%; width: 100%; backface-visibility: hidden; transform-style: preserve-3d; position: absolute; z-index: 0; transition: all 0.5s ease-out;"> <img src="img/source/'+img+'" alt="" style="backface-visibility: hidden;"> </div>');
        $("#flip-this").flip({
            trigger: 'hover'
        });
    });
    });

    });
    </script>

Try This, this may be because all function binding is on already loaded element if you want to load element dynamically you need to bind function to new loaded element also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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