当 jquery 生成的 css 加载完成时,jQuery 显示项目
有没有办法隐藏对象,直到我的脚本加载完成?使用 ajaxcomplete() 或 ajaxSuccess() 不起作用。下面是一个示例脚本。
有一个 id 为 imagefocus 的图像,每次我通过 ajax 更改图像时,您都会立即看到原始图像大小。
$("#imageFocus").bind("load", function(){
var width = $(this).width();
var height = $(this).height();
if(height > 443) {
var scaleRatio = height / 443,
width = Math.round(width / scaleRatio),
mLeft = Math.round((590 - width) / 2);
$(this).css({
height: "443px",
width: width+"px"
});
$(this).parent().css({
marginTop: "0px",
marginLeft: mLeft+"px"
});
}
}
Is there a way to hide an object until my script is done loading? Using ajaxcomplete() or ajaxSuccess() didn't work. Below an example script.
there is an image with the id: imagefocus, every time i change the image via ajax, you see for a split second the original image size.
$("#imageFocus").bind("load", function(){
var width = $(this).width();
var height = $(this).height();
if(height > 443) {
var scaleRatio = height / 443,
width = Math.round(width / scaleRatio),
mLeft = Math.round((590 - width) / 2);
$(this).css({
height: "443px",
width: width+"px"
});
$(this).parent().css({
marginTop: "0px",
marginLeft: mLeft+"px"
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过 css 隐藏图像,然后在其
load
事件中显示它。此外,在进行 ajax 调用隐藏图像之前,下面的代码将在图像加载后显示它。CSSJS
Hide the image through css and then show it on it's
load
event. Also before you make ajax call hide the image, the below code will take care of showing it once the image is loaded.Css
JS
是的,以:开始
并以(在您的加载函数中)结束
Yes, start with:
and end with (in your load function)
发生这种情况是因为加载完成并自动插入您的图像。您无法在实际回调中实现您想要的行为。相反,创建一个函数来隐藏图像,然后加载新图像。然后在回调中,使其再次可见
That happens because the load finishes and auto-inserts your image. You cannot achieve the behavior you want in the actualcallback. Instead create a function, that hides the image and after that loads the new one. Then in the callback, make it visible again