单击缩略图或链接创建放大效果

时间:2016-12-19 08:21:41

标签: javascript jquery html css

This link has a carousel similar to what I'm trying to implement 我有一个基本的图像滑块。

Something like this Example

jQuery(document).ready(function ($) {

  $('#checkbox').change(function(){
    setInterval(function () {
        moveRight();
    }, 3000);
  });
  
	var slideCount = $('#slider ul li').length;
	var slideWidth = $('#slider ul li').width();
	var slideHeight = $('#slider ul li').height();
	var sliderUlWidth = slideCount * slideWidth;
	
	$('#slider').css({ width: slideWidth, height: slideHeight });
	
	$('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
	
    $('#slider ul li:last-child').prependTo('#slider ul');

    function moveLeft() {
        $('#slider ul').animate({
            left: + slideWidth
        }, 200, function () {
            $('#slider ul li:last-child').prependTo('#slider ul');
            $('#slider ul').css('left', '');
        });
    };

    function moveRight() {
        $('#slider ul').animate({
            left: - slideWidth
        }, 200, function () {
            $('#slider ul li:first-child').appendTo('#slider ul');
            $('#slider ul').css('left', '');
        });
    };

    $('a.control_prev').click(function () {
        moveLeft();
    });

    $('a.control_next').click(function () {
        moveRight();
    });

});    
html {
  border-top: 5px solid #fff;
  background: #8A8A8A;
  color: #2a2a2a;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
}

h1 {
  color: #fff;
  text-align: center;
  font-weight: 300;
}

#slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
}

#slider ul {
  position: relative;
  margin: 0;
  padding: 0;
  height: 600px;
  list-style: none;
}

#slider ul li {
  position: relative;
  display: block;
  float: left;
  margin: 0;
  padding: 0;
  width: 1200px;
  height: 600px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

a.control_prev, a.control_next {
  position: absolute;
  top: 40%;
  z-index: 999;
  display: block;
  padding: 4% 3%;
  width: auto;
  height: auto;
  background: #2a2a2a;
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 18px;
  opacity: 0.8;
  cursor: pointer;
}

a.control_prev:hover, a.control_next:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

a.control_prev {
  border-radius: 0 2px 2px 0;
}

a.control_next {
  right: 0;
  border-radius: 2px 0 0 2px;
}

.slider_option {
  position: relative;
  margin: 10px auto;
  width: 160px;
  font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div id="slider">
  <a href="#" class="control_next">>></a>
  <a href="#" class="control_prev"><</a>
  <ul>
    <li>SLIDE 1</li>
    <li style="background: #fff;">SLIDE 2</li>
    <li>SLIDE 3</li>
    <li style="background: #fff;">SLIDE 4</li>
  </ul>  
</div>

<div class="slider_option">
  <input type="checkbox" id="checkbox">
  <label for="checkbox">Autoplay Slider</label>
</div> 

我希望在点击按钮/链接时打开滑块。我是jQuery的新手,所以我不确定是否可以使用onclick事件。有人可以告诉我这个实现背后的基本想法吗?谢谢!

2 个答案:

答案 0 :(得分:0)

在点击时添加类滑块并使用css类似

设置幻灯片元素的样式
element.active {
 position: fixed;
 top: 50px;
 left: 50%;
 transform: translateX(-50%);
 bottom: 50px;
  z-index: 9999;
}

然后元素将从幻灯片中弹出,您可以将任何想要的内容添加到元素的活动类中。并在您的示例中单击幻灯片元素

  $('#slider li').on('click', function () {
     $(this).toggleClass('active');
  });

活动类的css

li.active {
     position: fixed !important;
     top: 50px;
     left: 50%;
     transform: translateX(-50%);
     bottom: 50px;
}

用于简单叠加

li.active:after {
  content: '';
  position: fixed;
  top: -50px;
  bottom: -50px;
  background: rgba(0,0,0,0.75);
  left: -50%;
  width: 200%;
  height: 200%;
}

答案 1 :(得分:0)

在HTML中放置一个按钮,然后使用它在点击时显示slides。还要将完整的Slider相关HTML包装在div中并最初隐藏潜水。如下所示。

<button id="showSlider">Show me the slider</button>

<div id="sliderWrapper" style="display:none;">
 <!--Your current HTML goes here-->
</div>

有了这个,现在将脚本中的所有当前逻辑放在一个函数中。让我们点击按钮时调用此函数。

function SliderScripts() {
 //all your current scripts goes here
}

现在将文档就绪功能修改为以下。

jQuery(document).ready(function($) {

  $('#showSlider').on('click', function() { // button click event
    SliderScripts();  // run the sliders scripts
    $('#sliderWrapper').show(); / show the slider.
    $(this).hide(); //hide the button
  });

});

这是一个工作片段。

&#13;
&#13;
jQuery(document).ready(function($) {

  $('#showSlider').on('click', function() {
    SliderScripts();
    $('#sliderWrapper').show();
    $(this).hide();
  });

});

function SliderScripts() {
  $('#checkbox').change(function() {
    setInterval(function() {
      moveRight();
    }, 3000);
  });

  var slideCount = $('#slider ul li').length;
  var slideWidth = $('#slider ul li').width();
  var slideHeight = $('#slider ul li').height();
  var sliderUlWidth = slideCount * slideWidth;

  $('#slider').css({
    width: slideWidth,
    height: slideHeight
  });

  $('#slider ul').css({
    width: sliderUlWidth,
    marginLeft: -slideWidth
  });

  $('#slider ul li:last-child').prependTo('#slider ul');

  function moveLeft() {
    $('#slider ul').animate({
      left: +slideWidth
    }, 200, function() {
      $('#slider ul li:last-child').prependTo('#slider ul');
      $('#slider ul').css('left', '');
    });
  };

  function moveRight() {
    $('#slider ul').animate({
      left: -slideWidth
    }, 200, function() {
      $('#slider ul li:first-child').appendTo('#slider ul');
      $('#slider ul').css('left', '');
    });
  };

  $('a.control_prev').click(function() {
    moveLeft();
  });

  $('a.control_next').click(function() {
    moveRight();
  });

}
&#13;
html {
  border-top: 5px solid #fff;
  background: #8A8A8A;
  color: #2a2a2a;
}
html,
body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
}
h1 {
  color: #fff;
  text-align: center;
  font-weight: 300;
}
#slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
}
#slider ul {
  position: relative;
  margin: 0;
  padding: 0;
  height: 600px;
  list-style: none;
}
#slider ul li {
  position: relative;
  display: block;
  float: left;
  margin: 0;
  padding: 0;
  width: 1200px;
  height: 600px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}
a.control_prev,
a.control_next {
  position: absolute;
  top: 40%;
  z-index: 999;
  display: block;
  padding: 4% 3%;
  width: auto;
  height: auto;
  background: #2a2a2a;
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 18px;
  opacity: 0.8;
  cursor: pointer;
}
a.control_prev:hover,
a.control_next:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}
a.control_prev {
  border-radius: 0 2px 2px 0;
}
a.control_next {
  right: 0;
  border-radius: 2px 0 0 2px;
}
.slider_option {
  position: relative;
  margin: 10px auto;
  width: 160px;
  font-size: 18px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<button id="showSlider">Show me the slider</button>

<div id="sliderWrapper" style="display:none;">
  <div id="slider">
    <a href="#" class="control_next">>></a>
    <a href="#" class="control_prev">
    </a>
    <ul>
      <li>SLIDE 1</li>
      <li style="background: #fff;">SLIDE 2</li>
      <li>SLIDE 3</li>
      <li style="background: #fff;">SLIDE 4</li>
    </ul>
  </div>

  <div class="slider_option">
    <input type="checkbox" id="checkbox">
    <label for="checkbox">Autoplay Slider</label>
  </div>
</div>
&#13;
&#13;
&#13;