简体   繁体   English

如何使此JQuery滑块正确渲染幻灯片?

[英]How do I make this JQuery slider render slides properly?

Let me first begin by saying I am new to JQuery so I am guessing my way through this, which isn't ideal but I am going to invest more time learning stuff properly. 首先让我说我是JQuery的新手,所以我猜测我的方式,这不是理想的,但我会花更多的时间来正确地学习东西。

CODE: 码:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.core.js"></script>
    <script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.slide.js"></script>
<script type="text/javascript">
jQuery.fn.extend({
  slideRightShow: function(speed) {
    return this.each(function() {
        $(this).show('slide', {direction: 'right'}, +speed || 1000);
    });
  },
  slideLeftHide: function(speed) {
    return this.each(function() {
      $(this).hide('slide', {direction: 'left'}, +speed || 1000);
    });
  },
  slideRightHide: function(speed) {
    return this.each(function() {
      $(this).hide('slide', {direction: 'right'}, +speed || 1000);
    });
  },
  slideLeftShow: function(speed) {
    return this.each(function() {
      $(this).show('slide', {direction: 'left'}, +speed || 1000);
    });
  }
});

$(document).ready(function () {
    $(".left_hand_icon").on("click", function () {
        $("#slide1").slideLeftShow();
    });
});
$(document).ready(function () {
    $(".right_hand_icon").on("click", function () {
        $("#slide2").slideRightShow();
    });
});
</script>

HTML: HTML:

<div class="navigator">

<div class="left_hand_icon">Left</div>
<div class="right_hand_icon">Right</div>
</div>

<div id="slide1">Slide 1 content</div> <!-- displayed by default and i need to slide this away when the next slide is requested -->
<div id="slide2" style="display:none">Slide 2 content</div>
<div id="slide3" style="display:none">Slide 3 content</div>
<div id="slide4" style="display:none">Slide 4 content</div>

What I need help with? 我需要帮助的是什么?

Using my JS: 使用我的JS:

1) how can i on clicking left_hand_icon or right_hand_icon make the current content slide away and then show the next one? 1)如何点击left_hand_icon或right_hand_icon使当前内容滑出然后显示下一个? I am using display:none; 我正在使用display:none; to hide them but then how do I hide the current one when it's being displayed. 隐藏它们,但是当它显示时如何隐藏它们。 Can you help me head in the right direction with this? 你能帮助我朝着正确的方向前进吗?

2) When the page is loaded the first slide is showing. 2)加载页面时,显示第一张幻灯片。 How can I disable the ability for the user to click the left one since we are on the firs slide and how can I do the samething for the last slide where the next button shouldn't be able to be clicked since there is no slide to be shown? 如何禁用用户单击左侧的功能,因为我们在第一张幻灯片上,如何在最后一张幻灯片中执行相同的操作,因为没有幻灯片,因此无法点击下一个按钮被展示?

Can you help me head in the right direction with this? 你能帮助我朝着正确的方向前进吗?

In order to resolve the first issue what you can do is: 为了解决第一个问题,您可以做的是:

  • put all the image div elements in a parent container element, as in 将所有图像div元素放在父容器元素中,如
 <div id="container"> <div id="slide1">Slide 1 content</div> <div id="slide2" style="display:none">... </div> 
  • then you should apply CSS to parent container which will help you lay the given elements in proper inline manner and will form a long chain of images instead of showing one at a time. 然后你应该将CSS应用于父容器,这将帮助你以适当的内联方式放置给定元素,并将形成一长串图像,而不是一次显示一个。

Note: fix the width, height according to the image content, and set overflow to hidden so that rest of the images don't show up. 注意:根据图像内容修复宽度,高度,并将溢出设置为隐藏,以便其他图像不显示。

  • next all you have to do is use the jQuery's .animate() method on click of your images (left & right) and shift the current div of [.active] div approporiately eg. 接下来你只需要点击你的图像(左和右)使用jQuery的.animate()方法,然后适当地移动[.active] div的当前div。
 ... $('.active').animate({ left: '-280px'; //or the width + padding/margin of images. }); 

Coming to the second point, you can have a variable tht tracks the current object in focus (just the index) and if the index is equal to the number of 'div' elements present [$('div[id*="slide"]').length] you can disable the right button, and similarly left one for '0'th index. 来到第二点,你可以有一个变量跟踪当前焦点的对象(只是索引),如果索引等于'div'元素的数量[$('div [id * =“slide”] ]')。length]你可以禁用右键,同样留下一个'0'索引。

Rough Fiddle for the same is : http://jsfiddle.net/vsyg8/3/ 同样粗糙的小提琴是: http//jsfiddle.net/vsyg8/3/

hope this helps, and correct me if I am going wrong anywhere. 希望这有帮助,如果我在任何地方出错,请纠正我。

Part 1 第1部分

You already have the slideLeftHide etc. functions up and running, so all you need to do is figure out a way to apply them. 您已经启动并运行了slideLeftHide等功能,因此您需要做的就是找出应用它们的方法。 You could, for instance, add a currentSlide class to one of them and move that class around using the next() and prev() DOM traversal functions in jQuery. 例如,您可以向其中一个添加currentSlide类,并使用jQuery中的next()prev() DOM遍历函数移动该类。 That would let you select the current slide with a selector like $("#slides .currentSlide") if you wrap all your slides in a <div id="slides"> . 如果您将所有幻灯片包装在<div id="slides">那么您可以使用$("#slides .currentSlide")等选择器选择当前幻灯片。

Part 2 第2部分

You could do this with jQuery UI buttons : 您可以使用jQuery UI按钮执行此操作:

$(document).ready(function () {
    $(".left_hand_icon").button().click(function () {
        $("#slide1").slideLeftShow();
    }).button("disable");
});

and then call $(".left_hand_icon").button("enable") when you want to re-enable it. 然后在想要重新启用时调用$(".left_hand_icon").button("enable")

Here's a jsFiddle demonstrating the whole concept: http://jsfiddle.net/g5BA3/5/ - though the animation isn't particularly pretty. 这是一个展示整个概念的jsFiddle: http//jsfiddle.net/g5BA3/5/ - 尽管动画并不是特别漂亮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM