简体   繁体   English

滑块-您将如何制作一个简单的滑块?

[英]Slider - How would you make a simple slider?

EDIT: I WANT MY SLIDER TO BE EASY TO YOU AND ONCE A LINK IS CLICKED THE IMAGE THAT CORRELATES WITH LINK SHOWS. 编辑:我希望我的滑梯对您来说很容易,并且一旦链接被选中与链接显示相关的图像。

I'm looking to make a really simple "slider" that if you click a link, the img shows that correlates with it. 我正在寻找一个非常简单的“滑块”,如果您单击一个链接,该img将显示与之相关的链接。 I've been trying to find something for a bit now and things are either too flash or don't suit my needs. 我一直在尝试寻找一些东西,但是这些东西太闪光或不适合我的需求。 This came close: http://jsfiddle.net/bretmorris/ULNa2/7/ 这很接近: http : //jsfiddle.net/bretmorris/ULNa2/7/

I would something a little simpler that can be applied easily to multiple images for different divs. 我会更简单一些,可以轻松地将其应用于不同div的多个图像。

This is what my code looks like with just a plain img tag to it: 这是我的代码的样子,仅带有简单的img标签:

<div id="adobe_content" class="hdiv"><button class="x">X</button>
<img src="images/adobefront.png"><br>
<img src="images/adobeinside.png"><br>
<img src="images/adobeback.png"><br>
<h5>Adobe Brochure</h5>
<p>
I wanted to make something functional and out the box that Adobe might consider giving out. It's clean like their work and sparks interest in opening the brochure with the cut out in the center. The front flap is able to slide into a slot on the right side for a neat logo. They're now more interested in their cloud, but the information is inside is still relevant!
</p>
<b>Programs used: Adobe Illustrator, InDesign and Photoshop.</b>
</div>

The code doesn't work for me because, well I partially don't understand it, and I'm not sure how to make it suit my needs (especially if I got up to multiple images) like correlating with an image. 该代码对我不起作用,因为,好吧,我部分不理解它,而且我不确定如何使其适合自己的需求(尤其是当我获得多个图像时),例如与图像相关联。

Perhaps understanding what is going on would maybe get you on the right track, so here is an explanation: 也许了解正在发生的事情可能会使您步入正轨,所以这里有一个解释:

$('a.prev').click(function() {
    prevSlide($(this).parents('.slideshow').find('.slides'));
});
//clicking image goes to next slide
$('a.next, .slideshow img').click(function() {
    nextSlide($(this).parents('.slideshow').find('.slides'));
});

This part is relatively straightforward, when you click on the previous or next links, call the prevSlide or nextSlide function, passing the collection of slides as an argument. 这一部分相对简单,当您单击上一个或下一个链接时,调用prevSlidenextSlide函数,将幻灯片集合作为参数传递。

//initialize show
iniShow();

function iniShow() {
    //show first image
    $('.slideshow').each(function() {
        $(this).find('img:first').fadeIn(500);
    })
}

Initialize the slideshow by finding each slideshow on the page and fading in the first image. 通过在页面上找到每个幻灯片并在第一张图像中淡出来初始化幻灯片。 $(this) refers to the <div class="slideshow"> parent, find all child image tags and take the first, fade that element in (and do it in 500 milliseconds). $(this)引用<div class="slideshow">父对象,找到所有子图像标签,并获取第一个,淡入该元素(并在500毫秒内完成)。

function prevSlide($slides) {
    $slides.find('img:last').prependTo($slides);
    showSlide($slides);
}

function nextSlide($slides) {
    $slides.find('img:first').appendTo($slides);
    showSlide($slides);
}

The prevSlide and nextSlide functions both rearrange the order of images, this line in particular: prevSlidenextSlide函数都重新排列图像的顺序,特别是此行:

$slides.find('img:first').appendTo($slides);

Is moving the first image to the end of the images, so: 正在将第一个图像移到图像的末尾,因此:

 <img src="http://placekitten.com/300/500" width="300" height="500" />
 <img src="http://placekitten.com/200/400" width="200" height="400" />
 <img src="http://placekitten.com/400/400" width="500" height="400" />

becomes: 变为:

  <img src="http://placekitten.com/200/400" width="200" height="400" />
  <img src="http://placekitten.com/400/400" width="500" height="400" />
  <img src="http://placekitten.com/300/500" width="300" height="500" />

$slides.find('img:last').prependTo($slides); does the inverse and moves the last image to the beginning. 进行逆运算并将最后一个图像移到开头。

function showSlide($slides) {
    //hide (reset) all slides
    $slides.find('img').hide();
    //fade in next slide
    $slides.find('img:first').fadeIn(500);
}

Finally, showSlide accepts the collection of images, hides all of them and then fades in the first image (since the collection is reordered each time, the first is a different image). 最后, showSlide接受图像集合,将所有图像隐藏起来,然后在第一个图像中淡入淡出(由于每次都对集合进行重新排序,因此第一个图像是不同的图像)。

Now, if you want a link for each image that will display a corresponding image, you could do something as simple as: 现在,如果要为每个图像显示一个链接以显示对应的图像,则可以执行以下操作:

<a class="image" data-src="http://placekitten.com/300/500">Kitten 1</a>
<a class="image" data-src="http://placekitten.com/200/400">Kitten 2</a>
<a class="image" data-src="http://placekitten.com/400/500">Kitten 3</a>
<div id="image-container">
   <img src="http://placekitten.com/300/500" />
</div>

and something like the following: 如下所示:

$('.image').on('click', function() {
  var imageSrc = $(this).data('src');
  $('#image-container img').prop('src', imageSrc);
});

Which will update the child image tag of <div id="image-container"> with the data-src attribute value in the clicked link. 它将使用单击的链接中的data-src属性值更新<div id="image-container">的子图像标签。

http://jsfiddle.net/9sxt6n0t/ http://jsfiddle.net/9sxt6n0t/

Hope this helps. 希望这可以帮助。

just a quick function to slide 只是滑动的快捷功能

function slideIt(images , prev , next){
$('.slideshow img:nth-child(1)').show();
    var imagesLength = $(images).length;
var i = 1;
$(prev).click(function() {
    $(images).hide();
    if(i !== 1){
        $(images + ':nth-child('+ (i - 1) +')').show();
        i--;
    }else{
        $(images +':nth-child('+imagesLength +')').show();
        i = imagesLength;
    }
});
//clicking image goes to next slide
$(''+next+','+images+'').on('click',function() {
    $(images).hide();
    if(i !== imagesLength){
        $(images + ':nth-child('+ (i + 1) +')').show();
        i++;
    }else{
        $(images + ':nth-child(1)').show();
        i = 1;
    }
});
}

and use like that slideIt(Images , prevArrow , nextArrow) 并使用类似slideIt(Images,prevArrow,nextArrow)

slideIt('.slideshow img','a.prev' , 'a.next');

DEMO HERE 此处演示

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

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