使用hammer.js使克隆与html元素交换

问题描述 投票:0回答:1

我试图在两个div之间进行交换,我的意思是,通过其他保留事件和一切来改变。

两者都有锤子事件来检测滑动。当我在项目中创建交换元素时,他们丢失了锤子事件,但保留了其他事件,如点击。

为了说明我做的这个行为:

$(document).ready(function(){   
    $("#obj2").each(function(){
        var $this = $(this);
		var h = new Hammer(this);
		h.get("swipe").set({ direction: Hammer.DIRECTION_ALL, threshold: 1,  velocity:0.1 });
		h.on("swipe",function(ev){
			$this.text(ev.angle.toFixed(2));
        });
    }); 
    
    $("#obj1").click(function(){
      alert("this works");
    });
    
    $("#makeClone").click(function(){
        $("#obj2").text("3. swipe again :(");
        
        var aux = $("#obj2").clone(true);
        
        $("#container2").html($("#container1").clone(true));
        $("#container1").html(aux);
    });
});
div.obj,#makeClone{
    display:inline-block;
    box-sizing:border-box;
    width:300px;
    height:150px;
    text-align:center;
    border-radius:10px;
    background:#f44336;
    font:18px/150px "Lato";
    color:white;
    margin-bottom:5px;
    cursor:pointer;
}
#obj2{
    background:#4caf50;
}
#makeClone{
    background:#666;
    height:50px;
    font:12px/50px "hack";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
<script src="https://raw.githubusercontent.com/hammerjs/jquery.hammer.js/master/jquery.hammer.js"></script>

<div id="container1">
    <div id="obj1" class="obj">click</div>
</div>
<div id="container2">
    <div id="obj2" class="obj">1.swipe on me!</div>
</div>
<br><br>
<div id="makeClone">2.change with clone</div>
javascript jquery clone swipe hammer
1个回答
2
投票

不要clone你的对象。使用.append而不是.html

$(document).ready(function() {
  $("#obj2").each(function() {
    var $this = $(this);
    var h = new Hammer(this);
    h.get("swipe").set({
      direction: Hammer.DIRECTION_ALL,
      threshold: 1,
      velocity: 0.1
    });
    h.on("swipe", function(ev) {
      $this.text(ev.angle.toFixed(2));
    });
  });

  $("#obj1").click(function() {
    alert("this works");
  });

  $("#makeClone").click(function() {
    $("#obj2").text("3. swipe again :(");
    
    var d1 = $("#container1>div");
    var d2 = $("#container2>div");
    $("#container2").append(d1);
    $("#container1").append(d2);
    
  });
});
div.obj,
#makeClone {
  display: inline-block;
  box-sizing: border-box;
  width: 300px;
  height: 150px;
  text-align: center;
  border-radius: 10px;
  background: #f44336;
  font: 18px/150px "Lato";
  color: white;
  margin-bottom: 5px;
  cursor: pointer;
}

#obj2 {
  background: #4caf50;
}

#makeClone {
  background: #666;
  height: 50px;
  font: 12px/50px "hack";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>
<script src="https://raw.githubusercontent.com/hammerjs/jquery.hammer.js/master/jquery.hammer.js"></script>

<div id="container1">
  <div id="obj1" class="obj">click</div>
</div>
<div id="container2">
  <div id="obj2" class="obj">1.swipe on me!</div>
</div>
<br><br>
<div id="makeClone">2.change with clone</div>
© www.soinside.com 2019 - 2024. All rights reserved.