麻烦用CSS改变滑块上的起始位置

时间:2013-06-01 22:35:24

标签: javascript css navigation slider plane

我对CSS和Javascript非常陌生,并且作为一种项目一直在研究滑块,它在水平和垂直两个方向上移动。我使用this guide作为模型,并且有一些主要是可维护的东西。

但是,我希望滑块“开始”不是在标准原点(即最左边和最上面的幻灯片),而是在不同的,可自定义的点 - 例如,可用幻灯片的水平和垂直中心 - 我尝试的任何东西都不能帮助我做到这一点。我已经玩过边距,位置和填充,但是一切都只会让滑块混乱。有没有人知道如何更改页面加载中显示的幻灯片?

这是我到目前为止的CSS:

    .testprojbody
        {
        background-color: black;
        padding: 0;
        overflow: hidden;
        }
    .slider-holder
        {
        width: 98%;
        height: 665px;
        border: 2px black solid;
        background-color: white;
        float: center;
        margin-left: 9px;
        }
    .slider
        {
        width: 987px;
        height: 610px;
        overflow: hidden;
        float:center;
        margin-top: 25px;
        border: 2px black solid;
        margin-left: 35px;
        }
    .holder
        {
        width: 200%;
        height: 200%;
        position: relative;
        }
    .slide
        {
        float: left;
        width: 987px;
        height: 610px;
        position: relative;
        }
    .slider-navright 
        {
          text-align: center;
          margin: 310px 0 0 1030px;
          position: absolute;
        }
    .slider-navright a {
          width: 0;
          height: 0;
          display: inline-block;
          overflow: hidden;
          text-indent: -9999px;
          border-top: 40px solid transparent;
          border-bottom: 40px solid transparent;
          border-left: 20px solid #999;
        }
    .slider-navleft {
          text-align: center;
          margin: 310px 0 0 12px;
          position: absolute;
        }
    .slider-navleft a {
          width: 0;
          height: 0;
          display: inline-block;
          overflow: hidden;
          text-indent: -9999px;
          border-top: 40px solid transparent;
          border-bottom: 40px solid transparent;
          border-right: 20px solid #999;
        }
    .slider-navtop {
          text-align: center;
          margin: 2px 0 0 501px;
          position: absolute;
        }
    .slider-navtop a {
          width: 0;
          height: 0;
          display: inline-block;
          overflow: hidden;
          text-indent: -9999px;
          border-left: 40px solid transparent;
          border-right: 40px solid transparent;
          border-bottom: 20px solid #999;
        }
    .slider-navbot {
          text-align: center;
          margin: 642px 0 0 501px;
          position: absolute;
        }
    .slider-navbot a {
          width: 0;
          height: 0;
          display: inline-block;
          overflow: hidden;
          text-indent: -9999px;
          border-left: 40px solid transparent;
          border-right: 40px solid transparent;
          border-top: 20px solid #999;
        }

这是Javascript,允许导航&动画等:

<script type="text/javascript">
     var positionH = 0
     var positionV = 0
        $(document).ready(function(){
            var slider = {

  el: {
    slider: $("#slider"),
    allSlides: $(".slide"),
    sliderNavRight: $(".slider-navright"),
    sliderNavLeft: $(".slider-navleft"),
    sliderNavTop: $(".slider-navtop"),
    sliderNavBot: $(".slider-navbot"),
  },

  timing: 400,
  slideWidth: 987,
  slideHeight: 610,

  // In this simple example, might just move the
  // binding here to the init function
  init: function() {
    this.bindUIEvents();
  },

  bindUIEvents: function() {
    // nav code
    this.el.sliderNavRight.on("click", "a", function(event) {
      slider.handleNavRightClick(event, this);
    });
    this.el.sliderNavLeft.on("click", "a", function(event) {
      slider.handleNavLeftClick(event, this);
    });
    this.el.sliderNavTop.on("click", "a", function(event) {
      slider.handleNavTopClick(event, this);
    });
    this.el.sliderNavBot.on("click", "a", function(event) {
      slider.handleNavBotClick(event, this);
    });
  },


  handleNavRightClick: function(event, el) {
    positionH+=1;
    event.preventDefault();
    this.el.slider.animate({
      scrollLeft: this.slideWidth * positionH
    }, this.timing);
  },

  handleNavLeftClick: function(event, el) {
    positionH-=1;
    event.preventDefault();
    this.el.slider.animate({
      scrollLeft: this.slideWidth * positionH
    }, this.timing);
  },

  handleNavTopClick: function(event, el) {
    event.preventDefault();
    positionV--;
    this.el.slider.animate({
      scrollTop: this.slideHeight * positionV
    }, this.timing);
  },

  handleNavBotClick: function(event, el) {
    event.preventDefault();
    positionV++;
    this.el.slider.animate({
      scrollTop: this.slideHeight * positionV
    }, this.timing);
  }, 
};
slider.init();});
    </script>

  <script type="text/javascript">

//arrow functions
$(document.documentElement).keydown(function(event){
  if (event.keyCode == 39){
    //go right
    event.preventDefault();
    $('.slider-navright a')
      .click();
  } else if (event.keyCode == 37){
    //go left
    event.preventDefault();
    $('.slider-navleft a')
      .click();
  } else if (event.keyCode == 38){
    //go up
    event.preventDefault();
    $('.slider-navtop a')
      .click();
  } else if (event.keyCode == 40){
    //go down
    event.preventDefault();
    $('.slider-navbot a')
      .click();
  }
});

// makes slider unselectable AND makes arrow nav work better
$(".slider").disableSelection();

  </script>

这是相关的HTML:

<body class="testprojbody">
  <div class="slider-holder">
    <div class="slider" id="slider">
        <div class="holder">
            <div class="slide" id="slide-x0y0"></div>
            <div class="slide" id="slide-x1y0"></div>
            <div class="slide" id="slide-x0yA"></div>
        <div class="slide" id="slide-x0y0"></div>
        </div>
    </div>
    <nav class="slider-navright">
      <a href="#">Move Right</a>
    </nav>
  <nav class="slider-navleft">
    <a href="#">Move Left</a>
  </nav>
  <nav class="slider-navtop">
    <a href="#">Move Up</a>
  </nav>
  <nav class="slider-navbot">
    <a href="#">Move Down</a>
  </nav>
 </div>
</body>

</html>

希望这是可以理解的,正如我所说的,我很新(大约两周前才拿起javascript,而html可能是一个半月前),所以我确信这是非常草率的,迂回的代码。不过,如果有人能提供帮助,我们将不胜感激!

感谢。

1 个答案:

答案 0 :(得分:1)

您的JavaScript上的第一行看起来就像您正在寻找的那样。

您是否尝试过更改

的值?
var positionH = 0
var positionV = 0

到你想要的位置?

修改

好的,接下来你可以使用那些你需要的变量来为你的init方法添加更多的代码......

init: function() {
    this.bindUIEvents();
    this.el.slider.scrollLeft(positionH);
    this.el.slider.scrollTop(positionV);
}

然后更改positionH和positionV变量。