图片/图形

jQuery+css3移动滑块比较图像插件

阿里云


这是一款非常实用的 jQuery 和 css3 移动滑块比较图像插件。我们在 jquery 拖动滑块切换图片的图片切换插件和 jquery 通过滑块移动改变图片颜色插件都曾见过类似的效果。

当你制作了一个产品页面,有很多种创建图像效果的方法可以使用户“体验”你的产品。这款移动滑块比较图像插件就是其中之一。你可以去看看 Sony Ultra HD TV 产品页,他们也只用了这个图像特效来展示他们的产品。Google 在他们的 Google+ Photos 使用了这个特效。

也想出现在这里?联系我们
创客主机

HTML 结构

html 结构中使用一个 figure 作为包装容器包住原始图片、修改的图片和滑块。

  1. <figure class="cd-image-container">
  2.   <img src="img/img-original.jpg" alt="Original Image">
  3.   <span class="cd-image-label" data-type="original">Original</span>
  4.  
  5.   <div class="cd-resize-img"> <!-- the resizable image on top -->
  6.     <img src="img/img-modified.jpg" alt="Modified Image">
  7.     <span class="cd-image-label" data-type="modified">Modified</span>
  8.   </div>
  9.  
  10.   <span class="cd-handle"></span> <!-- slider handle -->
  11. </figure> <!-- cd-image-container -->

CSS 样式

.cd-resize-image 开始时宽度设置为 0,然后当它进入浏览器窗口时,宽度变为 50%。我们还使用 cd-bounce-in 动画来创建一种反弹效果。

  1. .cd-resize-img {
  2.   position: absolute;
  3.   top: 0;
  4.   left: 0;
  5.   height: 100%;
  6.   width: 0;
  7.   overflow: hidden;
  8.   /* Force Hardware Acceleration in WebKit */
  9.   transform: translateZ(0);
  10.   backface-visibility: hidden;
  11. }
  12. .is-visible .cd-resize-img {
  13.   width: 50%;
  14.   /* bounce in animation of the modified image */
  15.   animation: cd-bounce-in 0.7s;
  16. }
  17. @keyframes cd-bounce-in {
  18.   0% {
  19.     width: 0;
  20.   }
  21.   60% {
  22.     width: 55%;
  23.   }
  24.   100% {
  25.     width: 50%;
  26.   }
  27. }

JAVASCRIPT

为了实现滑块功能,我们为.cd-handle 元素提供了 drags()方法来使它可以被拖动。(参看 CSS-Tricks )当设备在.cd-handle 元素上按下并拖动时,我们根据当前鼠标的位置来更新.cd-handle 的 left 值。并且改变 div.cd-image-size 的宽度。为了添加移动设备的支持,我们使用了 jQuery mobile(注意:在触摸屏设备上使用 vmouse 事件来模拟 mouse 事件)。

  1. jQuery(document).ready(function($){
  2.     //function to check if the .cd-image-container is in the viewport here
  3.     // ...
  4.  
  5.     //make the .cd-handle element draggable and modify .cd-resize-img width according to its position
  6.     $('.cd-image-container').each(function(){
  7.         var actual = $(this);
  8.         drags(actual.find('.cd-handle'), actual.find('.cd-resize-img'), actual);
  9.     });
  10.  
  11.     //function to upadate images label visibility here
  12.     // ...
  13. });
  14.  
  15. //draggable funtionality - credits to http://css-tricks.com/snippets/jquery/draggable-without-jquery-ui/
  16. function drags(dragElement, resizeElement, container) {
  17.     dragElement.on("mousedown vmousedown", function(e) {
  18.         dragElement.addClass('draggable');
  19.         resizeElement.addClass('resizable');
  20.  
  21.         var dragWidth = dragElement.outerWidth(),
  22.             xPosition = dragElement.offset().left + dragWidth - e.pageX,
  23.             containerOffset = container.offset().left,
  24.             containerWidth = container.outerWidth(),
  25.             minLeft = containerOffset + 10,
  26.             maxLeft = containerOffset + containerWidth - dragWidth - 10;
  27.  
  28.         dragElement.parents().on("mousemove vmousemove", function(e) {
  29.             leftValue = e.pageX + xPosition - dragWidth;
  30.  
  31.             //constrain the draggable element to move inside its container
  32.             if(leftValue < minLeft ) {
  33.                 leftValue = minLeft;
  34.             } else if ( leftValue > maxLeft) {
  35.                 leftValue = maxLeft;
  36.             }
  37.  
  38.             widthValue = (leftValue + dragWidth/2 - containerOffset)*100/containerWidth+'%';
  39.  
  40.             $('.draggable').css('left', widthValue).on("mouseup vmouseup", function() {
  41.                 $(this).removeClass('draggable');
  42.                 resizeElement.removeClass('resizable');
  43.             });
  44.  
  45.             $('.resizable').css('width', widthValue); 
  46.  
  47.             //function to upadate images label visibility here
  48.             // ...
  49.  
  50.         }).on("mouseup vmouseup", function(e){
  51.             dragElement.removeClass('draggable');
  52.             resizeElement.removeClass('resizable');
  53.         });
  54.         e.preventDefault();
  55.     }).on("mouseup vmouseup", function(e) {
  56.         dragElement.removeClass('draggable');
  57.         resizeElement.removeClass('resizable');
  58.     });
  59. }

jQuery+css3 移动滑块比较图像插件

已有 345 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!