使用切换时,使第二个div位于第一个div的旁边

时间:2014-12-02 04:55:16

标签: javascript jquery toggle

切换时,第二个div首先出现在底部,然后出现在顶部。有没有办法让第二个div在切换时位于第一个div的左边?

提前致谢。

http://jsfiddle.net/KFmLv/1862/

HTML     

从右到左,从左到右滑动切换。

<hr/>
<p>
    <select class="mySelect">
        <option value="right">Right</option>
        <option value="left">Left</option>
        <option value="up">Up</option>
        <option value="down">Down</option>
    </select>
    <select class="mySelect2">
        <option value="left">Left</option>
        <option value="right">Right</option>
        <option value="up">Up</option>
        <option value="down">Down</option>
    </select>
    <button id="button" class="myButton">Toggle</button>
</p>
<div id="myDiv">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div id="myDiv2">
    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>

的javascript

$(".myButton").click(function () {

// Set the effect type
var effect = 'slide';

// Set the options for the effect type chosen
var options = {
    direction: $('.mySelect').val()
};
var options2 = {
    direction: $('.mySelect2').val()
};
// Set the duration (default: 400 milliseconds)
var duration = 500;

$('#myDiv').toggle(effect, options, duration);
$('#myDiv2').toggle(effect, options2, duration);

});

2 个答案:

答案 0 :(得分:1)

#myDiv#myDiv2包裹在具有相对位置的元素中,并将absolute位置提供给#myDiv#myDiv2

CSS:

#myDiv,#myDiv2{
    position:absolute;
    top:0;
}
.relative{
    position:relative;
}

HTML:

<div class="relative">
    <div id="myDiv">...</div>
    <div id="myDiv2">...</div>
</div>

See updated fiddle here.

答案 1 :(得分:0)

以下是更新后的jsfiddle,其中包含一些更改

'http://jsfiddle.net/KFmLv/1890/'