Dragdealer未初始化

时间:2018-08-25 14:20:29

标签: javascript plugins dragdealer-js

html

<div class="modal fade" id="exampleModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div id="demo-simple-slider" class="dragdealer">
        <div class="handle red-bar">
            <p><input id="values" onchange="alert();" type="text"></p>
        </div>
    </div>
</div>

jquery

$( "#exampleModal1" ).on('shown', function(){
    //Dragdealer
    //Product condition
    var main_width = $('#demo-simple-slider').outerWidth();

    new Dragdealer('demo-simple-slider', {
        animationCallback: function(x, y) {
            dragged_handle();
            $('#demo-simple-slider #values').val((x * 7).toFixed(1));
            var x = $('#demo-simple-slider #values').val();
        },

    });

    //Function to set Handle position
    function dragged_handle(){
        var left_off_container = $('#demo-simple-slider').offset().left;
        var left_off = $('#demo-simple-slider .handle').offset().left;
        var width_circle = ((left_off - left_off_container) + 10);
        $('#demo-simple-slider .sado-div').width(width_circle);
    }
});

我以前使用过此插件,并且效果很好。所以我使用了与上面显示的相同的代码,但是由于某种原因,句柄没有移动。控制台中没有错误,这就是为什么我找不到问题的原因。

1 个答案:

答案 0 :(得分:0)

您处在正确的轨道上。只需从$( "#exampleModal1" ).on('shown', function(){ ... })
更改显示的Bootstrap模态处理程序
$( "#exampleModal1" ).on('shown.bs.modal', function(){ ... })
请参阅Bootstrap Modal Events

您还将需要将处理程序包装在$(document).ready(function(){ ... })

最后,不要忘记包含相关的样式表和脚本。希望这会有所帮助

$(document).ready(function() {

  $("#exampleModal1").on('shown.bs.modal', function() {
//Dragdealer
//Product condition

var main_width = $('#demo-simple-slider').outerWidth();

new Dragdealer('demo-simple-slider', {
  animationCallback: function(x, y) {
    dragged_handle();
    $('#demo-simple-slider #values').val((x * 7).toFixed(1));
    var x = $('#demo-simple-slider #values').val();
  },

});

//Function to set Handle position
function dragged_handle() {
  var left_off_container = $('#demo-simple-slider').offset().left;
  var left_off = $('#demo-simple-slider .handle').offset().left;
  var width_circle = ((left_off - left_off_container) + 10);
  $('#demo-simple-slider .sado-div').width(width_circle);
}
  });

});
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dragdealer@0.10.0/src/dragdealer.css">
<script src="https://cdn.jsdelivr.net/npm/dragdealer@0.10.0/src/dragdealer.min.js"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal1">
  Launch demo modal
</button>

<div class="modal fade" id="exampleModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
<div class="modal-content">
  <div id="demo-simple-slider" class="dragdealer">
    <div class="handle red-bar">
      <p><input id="values" onchange="alert();" type="text"></p>
    </div>
  </div>
</div>
  </div>
</div>