Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跟vue结合,动态生成lightbox点击图片不展示大图 #52

Closed
blessyezi opened this issue Mar 3, 2017 · 2 comments
Closed

跟vue结合,动态生成lightbox点击图片不展示大图 #52

blessyezi opened this issue Mar 3, 2017 · 2 comments

Comments

@blessyezi
Copy link

代码如下

...
<div v-for="chat in chats">
    <span v-if="chat.media == 1">
        <img data-toggle="lightbox" v-bind:src="chat.s_imageUrl" class="img-thumbnail" alt="" v-bind:data-image="chat.imageUrl" data-caption="" />
    </span>
</div>
...
<script type="text/javascript">
    var vm = new Vue({
        el: '#chat',
        data: {
            chats: []
        }
    });
    var wsc = new WebSocket('ws://' + location.hostname + ':3000/cust-client/cust/connect?account='+account);
    wsc.onmessage = function(evt){
        vm.chats.push(evt.data)
    }
</script>

现在的情况是这样,如果我在data中初始化chats,比如data:{chats:[msgObject1]},图片能正常显示,点击图片也能展示大图,但是如果是在websocket中接收到message对象,再往data中塞的时候,图片也能正常显示,点击没有反应,console中也看不到异常信息,折腾了半天没搞定,这是我代码写的不对吗?

@catouse
Copy link
Collaborator

catouse commented Mar 3, 2017

Lightbox 只会自动初始化页面加载时出现在页面上的 [data-toggle="lightbox"] 元素作为触发元素。如果要为动态插入的内容启用 Lightbox 有两种方式:

  1. 在动态内容插入到页面后,选中动态插入的内容并调用 lightbox() 方法。
$('#newContents [data-toggle="lightbox"]').lightbox();
  1. 检测所有 [data-toggle="lightbox"] 元素上的点击事件,如果没有初始化则进行初始化并再次触发点击事件。
$(document).on('click', '[data-toggle="lightbox"]', function() {
    var $this = $(this);
    if(!$this.data('zui.lightbox')) {
        $this.lightbox().trigger('click');
    }
});

@blessyezi
Copy link
Author

非常感谢!搞定啦:+1:
由于vue是双向绑定,用第一种方法可能还需要加一个延时,所以使用了第二种方法.

@catouse catouse closed this as completed Mar 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants