如何使用.close()关闭jquery Lity?

时间:2018-02-08 20:11:25

标签: jquery

我使用Lity作为弹出窗体,但之后我无法使用.close()API关闭弹出窗口。如何通过使用“关闭”按钮调用.close()API来关闭Lity弹出窗口?提前谢谢!

$('a').on('click',function(){lity('#lity').opener();});
$('button').on('click',function(){
  //do something
  lity('#lity').close();	//this is not working
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.0/lity.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.0/lity.min.js"></script>

<a href="#">surprise</a>
<div id='lity' class='lity-hide'><button type='button'>close</button></div>

1 个答案:

答案 0 :(得分:1)

由于没有人回答,经过几天的奋斗后终于找到了2个解决方案。第一个解决方案是使用全局变量&#39; myLity&#39;并分配给“我的人”&#39;打开Lity时。然后使用相同的&#39; myLity&#39;关闭Lity。全局变量。

&#13;
&#13;
var myLity=null;
$('a').on('click',function(){
  myLity=lity('#lity');
  myLity.opener();
});
$('button').on('click',function(){
  //do something
  myLity.close();	//this is not working
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.0/lity.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.0/lity.min.js"></script>

<a href="#">surprise</a>
<div id='lity' class='lity-hide'><button type='button'>close</button></div>
&#13;
&#13;
&#13;

第二种解决方案更简单,只需添加数据关闭&#39;属性为关闭按钮。而且不需要全局变量。不管怎样,谢谢

&#13;
&#13;
$('a').on('click',function(){lity('#lity').opener();});
$('button').on('click',function(){
  //do something
  alert('executed');
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.0/lity.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lity/2.3.0/lity.min.js"></script>

<a href="#">surprise</a>
<div id='lity' class='lity-hide'><button type='button' data-lity-close>close</button></div>
&#13;
&#13;
&#13;