weixin_33698043 2017-12-25 02:01 采纳率: 0%
浏览 22

“初始化” jQuery操作?

So I found this library at GitHub called jTinder, which basically allows me to setup cards that can be left swiped or right swiped. I setup the #tinderslide div: <div id="tinderslide"><ul></ul></div>

I set up the cards using a function:

function generate_cards(){
    $.ajax({
        url:"fetch_cards.php",
        method:"POST",
        data:{"user":user_id},
        success:function(data){
            try{
                var parsedData=JSON.parse(data);
                for(var i=0;i<parsedData.length;i++){
                    var elem="<li class=\"card \"+parsedData[i].id>";
                    elem+="<div class=\"card_wrap\">";
                    elem+=<img src=\""+parsedData[i].picture.url+"\">";
                    elem+="<div class=\"name_location_wrapper\">";
                    elem+="<span class=\"name\">"+parsedData[i].first_name+" "+parsedData[i].last_name+"</span>";
                    elem+="<span class=\"location\"></span>";
                    elem+="</div></div></li>";
                    $("#tinderslide ul").prepend(elem);
                }
                dfd.resolve();
            }
            catch(er){
                console.log(er);
            }
        }
    })
}

and called the jTinder action:

function init(){
        $("#tinderslide").jTinder({
            onDislike: function (item) {
                $(".last").remove();
                $(".card").last().addClass("last");
            },
            onLike: function (item) {
                $(".last").remove();
                $(".card").last().addClass("last");
            },
            animationRevertSpeed: 100,
            animationSpeed: 100,
            threshold: 2
        });
    }

I have d3d set up as a $.Deferred variable and once it's resolved, I call the init() function.

I add ".last" to the last card for changing the background according to what the current object is, since the cards are arranged in reverse order, i.e. the last element is the first.

I then add the first batch of cards from a PHP file, which works fine. I can add the first 10 cards and store the responses just fine.

Next, I checked if the number of cards in the stack is less than 5, in which case, I fetch the next 10 cards. Here lies the problem.

Since the #tinderslide div is already initialized with the jTinder method, I can't initialize it again. The closest I've come to a solution is to check whether the card stack is empty, use $("#tinderslide").remove() to remove the #tinderslide div entirely, and then add it again, append the next cards and reinitialized, which seems to be working for now.

However, my question is: is there a more efficient way to do this, without having to delete and recreate the div entirely?

  • 写回答

1条回答 默认 最新

  • weixin_33708432 2017-12-25 04:25
    关注

    I added a destroy function to the Plugin.prototype:

    destroy: function(element){
        $(element).unbind();
        $(this.element).removeData();
    }
    

    and then called $("#tinderslide").data('plugin_jTinder').destroy();, which seems to work. Thanks to Roamer-1888 for the inputs!

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵