大神们 求救 ajax定时请求数据之后 就不执行前面js效果的 应该怎么解决啊

lingdianjiyi 2017-11-30 02:33:31
本人做的是留言板页面第一次加载正常显示 随机位置 可拖动 但是定时2秒请求之后 显示出来的数据都会 固定在左上角 并且不能拖动



下面帖源码



$(function(){
$('#cv img').load(function() {
var ig = ($(this).height());
var gaoa = $(window).height();
/* var gaob = gaoa-ig; */
var gaob = gaoa-ig-20;
$('#container').css('height',gaob);
});

});

var container;

// 可选颜色
var colors = ['url(/addons/amp_web/template/pingmu/img/1.png)', 'url(/addons/amp_web/template/pingmu/img/2.png)','url(/addons/amp_web/template/pingmu/img/3.png)', 'url(/addons/amp_web/template/pingmu/img/4.png)', 'url(/addons/amp_web/template/pingmu/img/5.png)', 'url(/addons/amp_web/template/pingmu/img/6.png)', 'url(/addons/amp_web/template/pingmu/img/7.png)', 'url(/addons/amp_web/template/pingmu/img/8.png)'];

//创建许愿页
var createItem = function(text){
var color = colors[parseInt(Math.random() * 5, 10)]
$('<div class="item">'+ text +'</div>').css({ 'background': color }).appendTo(container).drag();
};
/*<a href="#">关闭</a>*/
// 定义拖拽函数
$.fn.drag = function () {

var $this = $(this);
var parent = $this.parent();

var pw = parent.width();
var ph = parent.height();
var thisWidth = $this.width() + parseInt($this.css('padding-left'), 10) + parseInt($this.css('padding-right'), 10);
var thisHeight = $this.height() + parseInt($this.css('padding-top'), 10) + parseInt($this.css('padding-bottom'), 10);

var x, y, positionX, positionY;
var isDown = false;
$('#cv img').load(function() {
var ig = $(this).height();
var gaoa = $(window).height();
var gaob = gaoa-ig-20;
$('#container').css('height',gaob);
var h = $('#container').height();
var randY = parseInt(Math.random() * (h - thisHeight), 10);

var randX = parseInt(Math.random() * (pw - thisWidth), 10);


parent.css({
"position": "relative",
"overflow": "hidden"
});

$this.css({
"cursor": "move",
"position": "absolute"
}).css({
top: randY,
left: randX
}).mousedown(function (e) {
parent.children().css({
"zIndex": "0"
});
$this.css({
"zIndex": "1"
});
isDown = true;
x = e.pageX;
y = e.pageY;
positionX = $this.position().left;
positionY = $this.position().top;

return false;
});
});

$(document).mouseup(function (e) {
isDown = false;
}).mousemove(function (e) {
var xPage = e.pageX;
var moveX = positionX + xPage - x;

var yPage = e.pageY;
var moveY = positionY + yPage - y;
var h = $('#container').height();
var ch = h - thisHeight;
if (isDown == true) {
$this.css({
"left": moveX,
"top": moveY
});
} else {
return;
}
if (moveX < 0) {
$this.css({
"left": "0"
});
}
if (moveX > (pw - thisWidth)) {
$this.css({
"left": pw - thisWidth
});
}
if (moveY < 0) {
$this.css({
"top": "0"
});
}
if (moveY > ch) {
$this.css({
"top": ch
});
}
});
};
// 初始化
var init = function () {

container = $('#container');

// 绑定关闭事件
/*container.on('click','a',function () {
$(this).parent().remove();
}).height($(window).height() -204);*/
/* alert(1) */


/* function ajaxt(){ */
$.ajaxSettings.async = false;
var id = $('#shopids').val();
var url = $('#url').val();
var finid = $('#finid').val();
$.post(url , {id:id,finid:finid} , function(date){
if(date != 1){
$('#finid').val(date[0].finid)
/* alert($('#finid').val()) */

var str='';
$.each(date,function(k,v){
str+="'"+'<span class="zz"><span class="nc">'+v.openid+'</span><span class="sj">10:11:00</span></span><p>'+v.content+'</p>'+"',"
})
var basic = str.substr(0, str.length - 1);
var tests = "["+basic+"]";
var obj = eval('(' + tests + ')');
$.each(obj, function (i,v) {
createItem(v);
});
}

},'json')
$.ajaxSettings.async = true;
/* } */
/*超出部分...显示*/
$(document).ready(function() {
   $('#container p').ellipsis({row: 7});
})


};

$(function() {

init();

});
setInterval(function() {
init();
}, 2000);
...全文
270 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
a-shitou 2017-12-06
  • 打赏
  • 举报
回复
看到了,打断点看看你的h - thisHeight是不是0吧
a-shitou 2017-12-06
  • 打赏
  • 举报
回复
你的createItem 我只看到了随机生成颜色,并没看到随机设置位置啊

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧