左右滑动效果

时间:2010-08-31 08:30:26

标签: javascript jquery css

我有以下HTML代码:

<div class="menuTabs">
        <div class="mtabArrowLeft">Left</div>

       <input class="menutabBTN" name="" type="button" value="a" />
       <input class="menutabBTN" name="" type="button" value="b" />
       <input class="menutabBTN" name="" type="button" value="c" />
       <input class="menutabBTN" name="" type="button" value="d" />
       <input class="menutabBTN" name="" type="button" value="e" />
       <input class="menutabBTN" name="" type="button" value="f"/>
         <div class="mtabArrowRight">Right</div>
      </div>

我希望显示从A到D的前四个输入,当用户点击div时,mtabArrowLeft显示剩余的最多4个输入的隐藏输入。如果用户点击div mtabArrowRight将其反转。 我不知道该怎么做。

这是我的CSS代码:

.menuTabs {
    float: left;
    width: 537px;
}

.mtabArrowLeft {
    float: left;
    height: 25px;
    width: 35px;
    margin-left: 15px;
    margin-right: 4px;
}

.mtabArrowRight {
    float: left;
    height: 25px;
    width: 35px;
    margin-left: 3px;
    margin-right: 15px;

}

.menutabBTN {
    float: left;
    height: 25px;
    width: 65px;
    margin-right: 3px;
    margin-left: 3px;
    padding: 0px;
    border-top-width: 0px;
    border-right-width: 0px;
    border-bottom-width: 0px;
    border-left-width: 0px;
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 12px;
    color: #000;
    text-align: center;
    line-height: 25px;
}

提前感谢您的协助

2 个答案:

答案 0 :(得分:1)

您需要使用与此类似的方法: http://www.sohtanaka.com/web-design/automatic-image-slider-w-css-jquery/

您还可以看到用于在此处左右滑动图像缩略图的代码: http://www.skandium.com/product-viewer.asp?id=1400

基于第二个站点的代码为您的场景编写一些代码:

<!doctype html>
<html>
<head>
    <title></title>
    <style type="text/css">
.menuTabs {
    float: left;
    width: 284px;
    overflow:hidden;
    position:relative;
    height:50px;
}

.img-reel { position:absolute; left:0; top:0; height:50px; }

.mtabArrowLeft {
    float: left;
    height: 25px;
    width: 35px;
    margin-left: 15px;
    margin-right: 4px;
}

.mtabArrowRight {
    float: left;
    height: 25px;
    width: 35px;
    margin-left: 3px;
    margin-right: 15px;

}

.menutabBTN {
    float: left;
    height: 25px;
    width: 65px;
    margin-right: 3px;
    margin-left: 3px;
    padding: 0px;
    border-top-width: 0px;
    border-right-width: 0px;
    border-bottom-width: 0px;
    border-left-width: 0px;
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 12px;
    color: #000;
    text-align: center;
    line-height: 25px;
}

    </style>
</head>
<body>

<div class="mtabArrowLeft">Left</div>
<div class="menuTabs">
        <div class="img-reel">
            <input class="menutabBTN" name="" type="button" value="a" />
            <input class="menutabBTN" name="" type="button" value="b" />
            <input class="menutabBTN" name="" type="button" value="c" />
            <input class="menutabBTN" name="" type="button" value="d" />
            <input class="menutabBTN" name="" type="button" value="e" />
            <input class="menutabBTN" name="" type="button" value="f"/>
        </div>
</div>
<div class="mtabArrowRight">Right</div>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">


    $(function() {
            var imageWidth = 71;
            var reelSize = 4;
            var imageSum = $('.img-reel input').size();
            var imageReelWidth = imageWidth * imageSum;
            $('.img-reel').css({'width' : imageReelWidth});

            rotate = function(){
                var trigger = $btn.attr('class');
                var image_reelPosition = (trigger=='mtabArrowLeft') ? -imageWidth : imageWidth;
                var reel_currentPosition = $('.img-reel').css('left').replace('px','');
                var pos = reel_currentPosition-image_reelPosition;
                var maxPos = (imageSum-reelSize)*-imageWidth;
                //console.log('pos='+pos+', max='+maxPos);
                if(pos>=maxPos && pos<=0){
                    $('.img-reel').animate({left:pos},300);
                    $('.mtabArrowLeft,.mtabArrowRight').fadeTo(250,1);
                    //console.log('move');
                    if(pos==maxPos){$('.mtabArrowRight').fadeTo(250,0.5);}
                    else if(pos==0){$('.mtabArrowLeft').fadeTo(250,0.5);}
                }
            };
            if (imageSum > 4) {
                $('.mtabArrowLeft,.mtabArrowRight').click(function(){
                    $btn = $(this);
                    rotate();
                    return false;
                });
            }
            else {
                $('.mtabArrowLeft,.mtabArrowRight').fadeTo(0,0.5).click(function(){return false});
            }
    })
</script>
</body>
</html>

答案 1 :(得分:1)

http://flowplayer.org/tools/demos/scrollable/index.html

Scrollable是一个多功能且简单易用的滑块解决方案...从上面的链接中查看教程(并下载脚本) - 这个HTML / CSS / jQuery几乎直接来自网站。

您必须更改CSS中元素的宽度以适合您的设计。希望有所帮助。

<强> HTML

<div class="mtabArrowLeft prev left">Left</div>

    <div class="menuTabs scrollable">       
         <div class="items">
           <input class="menutabBTN" name="" type="button" value="a" />
           <input class="menutabBTN" name="" type="button" value="b" />
           <input class="menutabBTN" name="" type="button" value="c" />
           <input class="menutabBTN" name="" type="button" value="d" />
           <input class="menutabBTN" name="" type="button" value="e" />
           <input class="menutabBTN" name="" type="button" value="f"/>   
         </div>
    </div>

    <div class="mtabArrowRight next right">Right</div>

<强> CSS

.scrollable {
    position:relative;
    overflow:hidden;
    width: 660px;
    height:90px;
}

.scrollable .items {
    width:20000em;
    position:absolute;
}

.items input {
    float:left;
}

<强>的jQuery

$(function() {

    // initialize scrollable
    $(".scrollable").scrollable();

});