2010년 09월 03일
[JQUERY] 떠다니는 퀵메뉴 2편
그래서 화면을 늘이거나 줄여도 화면상에서 같은 위치에 있게 됩니다.
이번 거는 보이는 컨텐츠 내에서 위치가 고정되는 겁니다.
예를 들면 쇼핑몰 오른쪽에 최근 본 목록을 볼 때 항상 상품설명 밖에 위치하고 있고
거기서 상하로 스크롤 될 때도 위치가 고정되는 거죠.
그런 걸 만들어보고자 하는 것입니다.
<script type="text/javascript" language="Javascript">
jQuery(document).ready(function() {
jQuery(window).bind("resize", function() {
jQuery("#quick_menu_div").css("top", 100).css("left", ( document.body.clientWidth / 2) - 525).show();
}).trigger("resize");
b_width = $(document.body).width(); //페이지 전체크기
t_height = $(document.body).scrollTop(); //상단 높이
width = b_width/2 - 525;
height = t_height + 100; //상단부터 띄워야 하는 높이
$("#quick_menu_div").css({top:height, left:width, display:'block'});
var currentPosition = parseInt($("#quick_menu_div").css("top"));
$(window).scroll(function() {
var position = $(window).scrollTop(); // 현재 스크롤바의 위치값을 반환합니다.
$("#quick_menu_div").stop().animate({"top":position+currentPosition+"px"},1000);
});
});
</script>
이걸 head에 넣고
본문에 아래와 같이 넣어야 합니다.
<div id="quick_menu_div">
여기에 뭘 넣어야 하나?
</div>
마지막으로 css를 넣어줘야겠죠?
#quick_menu_div {
width: 150px;
height: 350px;
position: absolute;
background-color: #EEE;
top: 100px;
display: block;
z-index: 1000;
}
이렇게 하면 만들 수 있습니다.
상단 script에서 이런 식으로 function으로 만든 다음 원하는 값을 하나로 넣는 식으로 하면 관리가 훨씬 수월합니다.
jQuery(document).ready(function() {
quickMenu("#top_menu", 1, 515, 100);
function quickMenu(Div, leftRight, leftMargin, topMargin){
b_width = $(document.body).width(); //페이지 전체크기
t_height = $(document.body).scrollTop(); //상단 높이
if(leftRight == 1){
width = b_width/2 - leftMargin;
jQuery(window).bind("resize", function() {
jQuery(Div).css("top", topMargin).css("left", ( document.body.clientWidth / 2) - leftMargin).show();
}).trigger("resize");
} else {
width = b_width/2 + leftMargin;
jQuery(window).bind("resize", function() {
jQuery(Div).css("top", topMargin).css("left", ( document.body.clientWidth / 2) + leftMargin).show();
}).trigger("resize");
}
jQuery(window).bind("resize", function() {
jQuery(Div).css("top", topMargin).css("left", ( document.body.clientWidth / 2) - leftMargin).show();
}).trigger("resize");
height = t_height + topMargin; //상단부터 띄워야 하는 높이
$(Div).css({top:height, left:width, display:'block'});
var currentPosition = parseInt($(Div).css("top"));
$(window).scroll(function() {
var position = $(window).scrollTop(); // 현재 스크롤바의 위치값을 반환합니다.
$("#top_menu").stop().animate({"top":position+currentPosition+"px"},1000);
});
}
});
# by | 2010/09/03 16:42 | JAVASCRIPT | 트랙백 | 덧글(0)


