탭 js와 높이 맞추는 js를 함께 사용할 순 없을까요?
본문
탭 js와 블럭 높이 맞추는 js를 함께 사용할 순 없을까요?
사용중인 js는 아래와 같습니다.
탭 js
$(".tab li").click(function () {
var num = $(".tab li").index(this);
$(".tabContent").removeClass('active');
$(".tabContent").eq(num).addClass('active');
$(".tab li").removeClass('active');
$(this).addClass('active')
});
블럭 높이 맞추는 js (https://github.com/to-r/jquery.heightLine.js)
(function (factory) {
if(typeof module === "object" && typeof module.exports === "object") {
factory(require("jquery"), window, document);
} else {
factory(jQuery, window, document);
}
}(function($, window, document, undefined) {
$.fn.heightLine = function(){
var target = this,fontSizeChangeTimer,windowResizeId= Math.random();
var heightLineObj = {
op : {
"maxWidth" : 10000,
"minWidth" : 0,
"fontSizeCheck" : false
},
setOption : function(op){
this.op = $.extend(this.op,op);
},
destroy : function(){
target.css("height","");
},
create : function(op){
var self = this,
maxHeight = 0,
windowWidth = $(window).width();
self.setOption(op);
if( windowWidth<=self.op.maxWidth && windowWidth>=self.op.minWidth ){
target.each(function(){
if($(this).outerHeight()>maxHeight){
maxHeight = $(this).outerHeight();
}
}).each(function(){
var height = maxHeight
- parseInt($(this).css("padding-top"))
- parseInt($(this).css("padding-bottom"));
$(this).height(height);
});
}
},
refresh : function(op){
this.destroy();
this.create(op);
},
removeEvent :function(){
$(window).off("resize."+windowResizeId);
target.off("destroy refresh");
clearInterval(fontSizeChangeTimer);
}
}
if(typeof arguments[0] === "string" && arguments[0] === "destroy"){
target.trigger("destroy");
}else if(typeof arguments[0] === "string" && arguments[0] === "refresh"){
target.trigger("refresh");
}else{
heightLineObj["create"](arguments[0]);
$(window).on("resize."+windowResizeId,function(){
heightLineObj["refresh"]();
});
target.on("destroy",function(){
heightLineObj["removeEvent"]();
heightLineObj["destroy"]();
}).on("refresh",function(){
heightLineObj["refresh"]();
});
if(heightLineObj.op.fontSizeCheck){
if($("#fontSizeChange").length<=0){
var fontSizeChange = $("<span id='fontSizeChange'></span>").css({
width:0,
height:"1em",
position:"absolute",
left:0,
top:0
}).appendTo("body");
}
var defaultFontSize = $("#fontSizeChange").height();
fontSizeChangeTimer = setInterval(function(){
if(defaultFontSize != $("#fontSizeChange").height()){
heightLineObj["refresh"]();
}
},100);
}
}
return target;
}
}));
탭 전환으로 보여지는 컨텐츠를 달리 하고 있는데,
높이 맞추는 js를 사용할 시에 active된 탭 컨텐츠에는 적용이 되는데,
탭을 전환하면 높이가 0이 됩니다.
탭 전환시에도 높이를 맞추는 js를 사용할 수 있도록 할 수 있을까요?
위 내용만으로 해결할 수 있을까요?
html css부분도 올리는 것이 좋을까요?
!-->!-->답변 2
탭전환 스크립트안에 if문으로 높이맞추는쿼리를 추가하는건 어떤가요..?
예를들면
$(".tab li").click(function () {
var num = $(".tab li").index(this);
$(".tabContent").removeClass('active');
$(".tabContent").eq(num).addClass('active');
$(".tab li").removeClass('active');
$(this).addClass('active')
if(defined('.active')){function (factory) {
이런식으로 연결되게...?
아니면 블럭높이맞추는스크립트안에 difined로 무조건 액티브찾아서 바꾸게끔 만들거나....
미숙하지만 도움이되고싶네요...허허..
단순 탭 전환시 높이가 0이 안되면 되는건가요?
블럭 높이 맞추는거랑 상관없이 아래처럼 하면 안되나요
<style>
.tab li{display:inline-block;}
.tabContent{display:none; height:100px; border:1px solid}
.active {display:block;}
</style>
<ul class="tab">
<li>111111</li>
<li>22222</li>
<li>33333</li>
<li>44444</li>
</ul>
<div class="tabContent active">
1
</div>
<div class="tabContent">
2
</div>
<div class="tabContent">
3
</div>
<div class="tabContent">
4
</div>
<script type="text/javascript">
$(".tab li").click(function () {
var num = $(".tab li").index(this);
$(".tabContent").removeClass('active');
$(".tabContent").eq(num).addClass('active');
$(".tab li").removeClass('active');
$(this).addClass('active')
});
</script>