代码之家  ›  专栏  ›  技术社区  ›  Alina Ali

我有一个从左到右运行良好的滑块比较,但我希望它从上到下运行。我试图修改它,但没有结果。

  •  0
  • Alina Ali  · 技术社区  · 6 年前

    这是的代码(左右滑块比较) 有人能帮我把它从上到下工作吗? 我试图做些改变,但没有结果。 这是来自codepen的滑块,这是链接 https://codepen.io/bamf/pen/jEpxOX 谢谢您。

    // Call & init
    $(document).ready(function() {
      $('.ba-slider').each(function() {
        var cur = $(this);
        // Adjust the slider
        var width = cur.width() + 'px';
        cur.find('.resize img').css('width', width);
        // Bind dragging events
        drags(cur.find('.handle'), cur.find('.resize'), cur);
      });
    });
    
    // Update sliders on resize. 
    // Because we all do this: i.imgur.com/YkbaV.gif
    $(window).resize(function() {
      $('.ba-slider').each(function() {
        var cur = $(this);
        var width = cur.width() + 'px';
        cur.find('.resize img').css('width', width);
      });
    });
    
    function drags(dragElement, resizeElement, container) {
    
      // Initialize the dragging event on mousedown.
      dragElement.on('mousedown touchstart', function(e) {
    
        dragElement.addClass('draggable');
        resizeElement.addClass('resizable');
    
        // Check if it's a mouse or touch event and pass along the correct value
        var startX = (e.pageX) ? e.pageX : e.originalEvent.touches[0].pageX;
    
        // Get the initial position
        var dragWidth = dragElement.outerWidth(),
          posX = dragElement.offset().left + dragWidth - startX,
          containerOffset = container.offset().left,
          containerWidth = container.outerWidth();
    
        // Set limits
        var minLeft = containerOffset + 10;
        var maxLeft = containerOffset + containerWidth - dragWidth - 10;
    
        // Calculate the dragging distance on mousemove.
        dragElement.parents().on("mousemove touchmove", function(e) {
    
          // Check if it's a mouse or touch event and pass along the correct value
          var moveX = (e.pageX) ? e.pageX : e.originalEvent.touches[0].pageX;
    
          var leftValue = moveX + posX - dragWidth;
    
          // Prevent going off limits
          if (leftValue < minLeft) {
            leftValue = minLeft;
          } else if (leftValue > maxLeft) {
            leftValue = maxLeft;
          }
    
          // Translate the handle's left value to masked divs width.
          var widthValue = (leftValue + dragWidth / 2 - containerOffset) * 100 / containerWidth + '%';
    
          // Set the new values for the slider and the handle. 
          // Bind mouseup events to stop dragging.
          $('.draggable').css('left', widthValue).on('mouseup touchend touchcancel', function() {
            $(this).removeClass('draggable');
            resizeElement.removeClass('resizable');
          });
          $('.resizable').css('width', widthValue);
        }).on('mouseup touchend touchcancel', function() {
          dragElement.removeClass('draggable');
          resizeElement.removeClass('resizable');
        });
        e.preventDefault();
      }).on('mouseup touchend touchcancel', function(e) {
        dragElement.removeClass('draggable');
        resizeElement.removeClass('resizable');
      });
    }
    .ba-slider {
      position: relative;
      overflow: hidden;
    }
    
    .ba-slider img {
      width: 100%;
      display: block;
    }
    
    .resize {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 50%;
      overflow: hidden;
    }
    
    .handle {
      /* Thin line seperator */
      position: absolute;
      left: 50%;
      top: 0;
      bottom: 0;
      width: 4px;
      margin-left: -2px;
      background: rgba(0, 0, 0, 0.5);
      cursor: ew-resize;
    }
    
    .handle:after {
      /* Big orange knob  */
      position: absolute;
      top: 50%;
      width: 64px;
      height: 64px;
      margin: -32px 0 0 -32px;
      content: '\21d4';
      color: white;
      font-weight: bold;
      font-size: 36px;
      text-align: center;
      line-height: 64px;
      background: #ffb800;
      /* @orange */
      border: 1px solid #e6a600;
      /* darken(@orange, 5%) */
      border-radius: 50%;
      transition: all 0.3s ease;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3), inset 0 2px 0 rgba(255, 255, 255, 0.5), inset 0 60px 50px -30px #ffd466;
      /* lighten(@orange, 20%)*/
    }
    
    .draggable:after {
      width: 48px;
      height: 48px;
      margin: -24px 0 0 -24px;
      line-height: 48px;
      font-size: 30px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="ba-slider">
      <img src="https://i.imgur.com/9JWNlfH.jpg" alt="">
      <div class="resize">
        <img src="https://i.imgur.com/GS86OpZ.jpg" alt="">
      </div>
      <span class="handle"></span>
    </div>

    JS+CSS代码在codepen上..我无法在这里加载它(抱歉)

    1 回复  |  直到 6 年前
        1
  •  1
  •   WizardCoder    6 年前

    我真的不应该这样做,因为这是一个编码变更而不是编码问题,但我对如何做感兴趣。

    我把所有的宽度计算都改成了高度,所有的位置都从左到上。我也对CSS做了类似的调整。

    // Call & init
    $(document).ready(function(){
      $('.ba-slider').each(function(){
        var cur = $(this);
        // Adjust the slider
        var height = cur.height()+'px';
        cur.find('.resize img').css('height', height);
        // Bind dragging events
        drags(cur.find('.handle'), cur.find('.resize'), cur);
      });
    });
    
    // Update sliders on resize. 
    // Because we all do this: i.imgur.com/YkbaV.gif
    $(window).resize(function(){
      $('.ba-slider').each(function(){
        var cur = $(this);
        var height = cur.height()+'px';
        cur.find('.resize img').css('height', height);
      });
    });
    
    function drags(dragElement, resizeElement, container) {
    	
      // Initialize the dragging event on mousedown.
      dragElement.on('mousedown touchstart', function(e) {
        
        dragElement.addClass('draggable');
        resizeElement.addClass('resizable');
        
        // Check if it's a mouse or touch event and pass along the correct value
        var startY = (e.pageY) ? e.pageY : e.originalEvent.touches[0].pageY;
        
        // Get the initial position
        var dragHeight = dragElement.outerHeight(),
            posY = dragElement.offset().top + dragHeight - startY,
            containerOffset = container.offset().top,
            containerHeight = container.outerHeight();
     
        // Set limits
        minTop = containerOffset + 10;
        maxTop = containerOffset + containerHeight - dragHeight - 10;
        
        // Calculate the dragging distance on mousemove.
        dragElement.parents().on("mousemove touchmove", function(e) {
        	
          // Check if it's a mouse or touch event and pass along the correct value
          var moveY = (e.pageY) ? e.pageY : e.originalEvent.touches[0].pageY;
          
          topValue = moveY + posY - dragHeight;
          
          // Prevent going off limits
          if ( topValue < minTop) {
            topValue = minTop;
          } else if (topValue > maxTop) {
            topValue = maxTop;
          }
          
          // Translate the handle's left value to masked divs height.
          heightValue = (topValue + dragHeight/2 - containerOffset)*100/containerHeight+'%';
    			
          // Set the new values for the slider and the handle. 
          // Bind mouseup events to stop dragging.
          $('.draggable').css('top', heightValue).on('mouseup touchend touchcancel', function () {
            $(this).removeClass('draggable');
            resizeElement.removeClass('resizable');
          });
          $('.resizable').css('height', heightValue);
        }).on('mouseup touchend touchcancel', function(){
          dragElement.removeClass('draggable');
          resizeElement.removeClass('resizable');
        });
        e.preventDefault();
      }).on('mouseup touchend touchcancel', function(e){
        dragElement.removeClass('draggable');
        resizeElement.removeClass('resizable');
      });
    }
    .ba-slider {
        position: relative;
        overflow: hidden;
    }
     
    .ba-slider img {
        width: 100%;
        display:block;
    }
     
    .resize {
        position: absolute;
        top:0;
        left: 0;
        height: 50%;
        width: 100%;
        overflow: hidden;
    }
    
    
    .handle { /* Thin line seperator */
      position:absolute; 
      left:0;
      top:50%;
      bottom:0;
      width:100%;
      height:4px;
      margin-left:-2px;
     
      background: rgba(0,0,0,.5);
      cursor: ns-resize;
    }
     
    .handle:after {  /* Big orange knob  */
        position: absolute;
        top: 50%;
        left:50%;
        width: 64px;
        height: 64px;
        margin: -32px 0 0 -32px;
     
        content:'\21D5';
        color:white;
        font-weight:bold;
        font-size:36px;
        text-align:center;
        line-height:64px;
     
        background: #ffb800; /* @orange */
        border:1px solid #e6a600; /* darken(@orange, 5%) */
        border-radius: 50%;
        transition:all 0.3s ease;
        box-shadow:
          0 2px 6px rgba(0,0,0,.3), 
          inset 0 2px 0 rgba(255,255,255,.5),
          inset 0 60px 50px -30px #ffd466; /* lighten(@orange, 20%)*/ 
    }
    
    .draggable:after {
        width: 48px;
        height: 48px;
        margin: -24px 0 0 -24px;
        line-height:48px;
        font-size:30px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="ba-slider">
      <img src="https://placeimg.com/640/480/animals" alt="">       
      <div class="resize">
        <img src="https://placeimg.com/640/480/people" alt="">
      </div>
      <span class="handle"></span>
    </div>