プロが教える店舗&オフィスのセキュリティ対策術

やりたい事は

(1)
L・Rという2つのラジオボタンを設置し、

ウィンドウ左に配置したDIV要素のボックスを、

Lをクリックでウィンドウ左、

Rをクリックでウィンドウ右に配置を変更させる。

(2)
画像ボタンを設置し、クリックされると

DIV要素のボックスを、ウィンドウ枠外へスライドさせ、

再クリックで元の配置にスライドさせ戻したい。


初心者のためスクリプトをどのように記述すれば、

良いか解らず質問させていただきます。

ご教授よろしくお願いします。

http://www11.ocn.ne.jp/~website/sample3/index.html

一応参考アドレスを記載させていただきますが

この記述方法だと、DIV要素のボックスが左側にあるとき、

画像ボタンクリックで、DIV要素のボックスを、

ウィンドウ枠外へスライドさせ、

再クリックで元の配置に戻すと、

その後、Rをクリックしても、DIV要素のボックスが

ウインドウ右側へ移動しなくなってしまいます。

===== HTML ====

<body>
<div id="wrapper">
<header></header>
<nav></nav>
<div id="box" class="box_l">
<label>
<input type="radio" id="radio1" name="test" checked="checked" />
L</label>
<label>
<input type="radio" id="radio2" name="test" />
R</label>
</div>
<div id="tab" class="tab_l"></div>
<section id="section01" class="section">
<p>PAGE1</p>
</section>
</div>
<div id="page_top"></div>
<footer></footer>
</body>

==== CSS ====

.box_l {
position: fixed;
top:30px;
left:0px;
z-index: 2000;
width: 90px;
height: 160px;
padding: 20px 5px 5px 5px;
background-color: rgba(0,0,0,0.25);
}
.box_r {
position: fixed;
top:30px;
right:0px;
z-index: 2000;
width: 90px;
height: 160px;
padding: 20px 5px 5px 5px;
background-color: rgba(0,0,0,0.25);
}
.tab_l {
position: fixed;
top:30px;
left:100px;
z-index: 2000;
width: 30px;
height: 185px;
background-color: rgba(0,0,0,0.25);
background-image: url(../images/tnm2.png);
background-position: center center;
background-repeat: no-repeat;
cursor: pointer;
border-radius: 0 10px 10px 0; /* CSS3草案 */
-webkit-border-radius: 0 10px 10px 0; /* Safari,Google Chrome用 */
-moz-border-radius: 0 10px 10px 0; /* Firefox用 */
}
.tab_r {
position: fixed;
top:30px;
right:100px;
z-index: 2000;
width: 30px;
height: 185px;
background-color: rgba(0,0,0,0.25);
background-image: url(../images/tnm1.png);
background-position: center center;
background-repeat: no-repeat;
cursor: pointer;
border-radius: 10px 0 0 10px; /* CSS3草案 */
-webkit-border-radius: 10px 0 0 10px; /* Safari,Google Chrome用 */
-moz-border-radius: 10px 0 0 10px; /* Firefox用 */
}
#section01 {
min-height: 100%;
min-width:320px;
width: 100%;
height: 1000px;
}
#section01 p {
font-style: normal;
font-weight: 400;
color: #000;
font-size: 48px;
text-align: center;
}

==== SCRIPT ====

$(window).on('load', function() {
for (i = 1; i <= 2; i++) {
document.getElementById('radio' + i).checked = i==1;
}
});

$(function() {
$("[name='test']").click(function(){
var num = $("[name='test']").index(this);
if(num == 1){
$("#box").removeClass("box_l").addClass("box_r");
$("#tab").removeClass("tab_l").addClass("tab_r");
} else {
$("#box").removeClass("box_r").addClass("box_l");
$("#tab").removeClass("tab_r").addClass("tab_l");
}
})

$(".tab_l,.tab_r").toggle(function(){
$(".box_l").animate({'left':'-100px'},300);
$(".tab_l").css('background-image','url(images/tnm1.png)').animate({'height':'40px','left':'0px'},300);
$(".box_r").animate({'right':'-100px'},300);
$(".tab_r").css('background-image','url(images/tnm2.png)').animate({'height':'40px','right':'0px'},300);
},
function(){
$(".box_l").animate({'left':'0px'},300);
$(".tab_l").css('background-image','url(images/tnm2.png)').animate({'height':'185px','left':'100px'},300);
$(".box_r").animate({'right':'0px'},300);
$(".tab_r").css('background-image','url(images/tnm1.png)').animate({'height':'185px','right':'100px'},300);
})
})

A 回答 (1件)

$("[name='test']").click(function(){


var num = $("[name='test']").index(this);
if(num == 1){
$("#box").removeClass("box_l").addClass("box_r").removeAttr('style');
$("#tab").removeClass("tab_l").addClass("tab_r").removeAttr('style');
} else {
$("#box").removeClass("box_r").addClass("box_l").removeAttr('style');
$("#tab").removeClass("tab_r").addClass("tab_l").removeAttr('style');
}
})

とすればとりあえずうまく動くようです。
animateやcssであてたstyleがclassよりも優先されるのでそのような動作になるようです。

animationをcssだけで表現すればclassのaddとremoveでもっとかんたんに書けるはず。
    • good
    • 0
この回答へのお礼

早速の返信ありがとうございます。

回答に記載して戴いたスクリプトで動作確認出来ました。

今回の回答大変参考になりました。

ありがとうございました。

お礼日時:2014/08/20 03:30

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!