创建有效的缩放,以单击缩略图或链接



此链接具有类似于我要实现的旋转频率我有一个基本的图像滑块。

类似此示例的东西

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活动。有人可以告诉我这个实施背后的基本思想吗?谢谢!

在您的HTML中放置一个按钮,并使用它在单击时显示您的slides。还将您完整的滑块相关的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
  });
});

这是一个工作片段。

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();
  });
}
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>
<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>

在单击上添加滑块,并用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%;
}

最新更新