求教,关于JQ使用hide()后无法获取元素的高度和宽度

easeyoo12 2014-03-29 02:21:18
如题,求各位帮帮忙,就是JQ使用hide()方法隐藏后,另一段程序就无法获取元素的宽度和高度了

$.fn.jCarouselLite = function(o) {
o = $.extend({
btnPrev: null,
btnNext: null,
btnGo: null,
mouseWheel: false,
auto: null,

speed: 200,
easing: null,

vertical: false,
circular: true,
visible: 5,
start: 0,
scroll: 1,

beforeStart: null,
afterEnd: null
}, o || {});

return this.each(function() {

var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width"; //问题应该是出在这
var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

if(o.circular) {
ul.prepend(tLi.slice(tl-v-1+1).clone())
.append(tLi.slice(0,v).clone());
o.start += v;
}

var li = $("li", ul), itemLength = li.size(), curr = o.start;
div.css("visibility", "visible");

li.css({overflow: "hidden", float: o.vertical ? "none" : "left"});
ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});

var liSize = o.vertical ? height(li) : width(li); // Full li size(incl margin)-Used for animation
var ulSize = liSize * itemLength; // size of full ul(total length, not just for the visible items)
var divSize = liSize * v; // size of entire div(total length for just the visible items)

li.css({width: li.width(), height: li.height()});
ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));

div.css(sizeCss, divSize+"px"); // Width of the DIV. length of visible images

if(o.btnPrev)
$(o.btnPrev).click(function() {
return go(curr-o.scroll);
});

if(o.btnNext)
$(o.btnNext).click(function() {
return go(curr+o.scroll);
});

if(o.btnGo)
$.each(o.btnGo, function(i, val) {
$(val).click(function() {
return go(o.circular ? o.visible+i : i);
});
});

if(o.mouseWheel && div.mousewheel)
div.mousewheel(function(e, d) {
return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
});

if(o.auto)
setInterval(function() {
go(curr+o.scroll);
}, o.auto+o.speed);

function vis() {
return li.slice(curr).slice(0,v);
};

function go(to) {
if(!running) {

if(o.beforeStart)
o.beforeStart.call(this, vis());

if(o.circular) { // If circular we are in first or last, then goto the other end
if(to<=o.start-v-1) { // If first, then goto last
ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
// If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
} else if(to>=itemLength-v+1) { // If last, then goto first
ul.css(animCss, -( (v) * liSize ) + "px" );
// If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
} else curr = to;
} else { // If non-circular and to points to first or last, we just return.
if(to<0 || to>itemLength-v) return;
else curr = to;
} // If neither overrides it, the curr will still be "to" and we can proceed.

running = true;

ul.animate(
animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
function() {
if(o.afterEnd)
o.afterEnd.call(this, vis());
running = false;
}
);
if(!o.circular) {
$(o.btnPrev + "," + o.btnNext).removeClass("disabled");
$( (curr-o.scroll<0 && o.btnPrev)
||
(curr+o.scroll > itemLength-v && o.btnNext)
||
[]
).addClass("disabled");
}

}
return false;
};
});
};

请教如何能让这段程序获取到元素的宽高,如果不使用hide()方法可以吗,还有什么方法能替代hide()做到既能隐藏元素又能让程序获取到元素的宽高,谢谢。
...全文
364 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
旧版的Jquery可能存在这个问题 如下获取是可以的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.box{width:100px;height:100px;background:red;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function(){
	$('#btn').click(function(){
		$('.box').toggle();
	});
	
	$('#btn1').click(function(){
		alert("宽度:"+$('.box').width());
	});
})

</script>
</head>


<body>
<div class="box">
</div>
<input type="button" value="显示或隐藏" id="btn"/>
<input type="button" value="获取宽高" id="btn1"/>
</body>
</html>
easeyoo12 2014-03-29
  • 打赏
  • 举报
回复
引用 1 楼 Return_false 的回复:
获取元素宽高时,判断该元素是否隐藏,如果隐藏,修改为显示,获取宽高后,再按照之前的恢复为隐藏即可
感谢回复,只是那个获取宽高的代码是引用的JQ插件,看不懂他的原理,麻烦能写一段示例吗,谢谢。
  • 打赏
  • 举报
回复
获取元素宽高时,判断该元素是否隐藏,如果隐藏,修改为显示,获取宽高后,再按照之前的恢复为隐藏即可

87,910

社区成员

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

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