11

こんにちは世界中の開発者。

シンプルで純粋なJavascript (30行)、JQueryフリー(​​および他のライブラリ)の ドラッグコントロールスライダーについてサポートが必要です。

私は何ヶ月も検索していて、多くのスクリプトを見つけましたが、-Jqueryが好きではありません。ほとんどのスクリプトには4、5、6のjavascriptが含まれています。小さなスクリプトから多くのことを学びます。

必要なのは、画像の再スケーリング、ページのスクロール、画像の明るさの変更(PHPを使用)などに使用できるシンプルなスライダーだけです。

私はjavascript (2か月)を初めて使用します。これが今のところです。変数名が間違っていることをお詫びします。


    <script type="text/javascript">  
      _item = null;
      mouse_x = 0;
      drag_x = 0; 
      function move_init() {
        document.onmousemove = _move;
        document.onmouseup = _stop;
      }
      function _stop(){
         _item = null;
      }
      function _move(e){
        mouse_x = document.all ? window.event.clientX : e.pageX;
        if(_item != null){
             _item.style.left = (mouse_x - drag_x) + "px";
        }
      }
      function _move_item(drag)
      {
        _item = drag;
        drag_x = mouse_x - _item.offsetLeft;
      }
move_init();
drag.onmousedown=_move_item();   // Agh.. did'nt figure out how this works
</script>

<style type="text/css">
#drag{background:#797979;color:#fff;width:30px;height:15px;position:relative;}
#track{background:red; width:200px;}
</style>

<div id="track"><div id="drag" onmousedown="_move_item(this);" >x</div></div>

私はあなたの助けに感謝します。

私は2012年12月31日にこれを書きました。とても幸せな新年。お互いに仲良くしてください。

ありがとうございました。

4

2 に答える 2

27

このコードは最新のブラウザで機能します。そのためのポリフィルを作成するだけでaddEventListener、このカスタム範囲スライダーを安全に使用できます。

function rangeSlider(id, onDrag) {

    var range = document.getElementById(id),
        dragger = range.children[0],
        draggerWidth = 10, // width of your dragger
        down = false,
        rangeWidth, rangeLeft;

    dragger.style.width = draggerWidth + 'px';
    dragger.style.left = -draggerWidth + 'px';
    dragger.style.marginLeft = (draggerWidth / 2) + 'px';

    range.addEventListener("mousedown", function(e) {
        rangeWidth = this.offsetWidth;
        rangeLeft = this.offsetLeft;
        down = true;
        updateDragger(e);
        return false;
    });

    document.addEventListener("mousemove", function(e) {
        updateDragger(e);
    });

    document.addEventListener("mouseup", function() {
        down = false;
    });

    function updateDragger(e) {
        if (down && e.pageX >= rangeLeft && e.pageX <= (rangeLeft + rangeWidth)) {
            dragger.style.left = e.pageX - rangeLeft - draggerWidth + 'px';
            if (typeof onDrag == "function") onDrag(Math.round(((e.pageX - rangeLeft) / rangeWidth) * 100));
        }
    }

}

使用例

<style>
.range-slider {
  width:300px;
  height:20px;
  margin:0 auto 1em;
  position:relative;
  cursor:e-resize;
}
.range-slider:before {
  content:"";
  display:block;
  position:absolute;
  top:9px;
  left:0;
  width:100%;
  height:2px;
  background-color:black;
}
.range-slider span {
  display:block;
  height:inherit;
  position:relative;
  z-index:2;
  background-color:red;
  cursor:inherit;
}
</style>

<div class="range-slider" id="range-slider-1">
  <span></span>
</div>

<script>
rangeSlider('range-slider-1', function(value) {
    document.getElementById('test-area').innerHTML = value + '%';
});
</script>

デモ: http: //jsbin.com/dulifezi/2/edit


アップデート

ここでこのスニペットのリポジトリを作成しています→ https://github.com/taufik-nurrohman/range-slider

于 2014-03-31T13:22:14.217 に答える
2

DragDealer.jsを見てください: https ://skidding.github.io/dragdealer/

ここに、スライダーの値に基づいて画像の不透明度を変更する例があります。

お役に立てれば!

于 2013-04-24T21:25:49.477 に答える