Skip to content

Instantly share code, notes, and snippets.

@ufologist
Created December 17, 2015 04:55
Show Gist options
  • Save ufologist/12c332442f6e32659508 to your computer and use it in GitHub Desktop.
Save ufologist/12c332442f6e32659508 to your computer and use it in GitHub Desktop.
jQuery UI Draggable click
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>jQuery UI Draggable click</title>
<style>
body {
background-color: #eee;
}
.panel {
position: relative;
margin-bottom: 20px;
border: 1px solid #ddd;
background-color: #fff;
}
.panel-hd,
.panel-bd {
padding: 10px 15px;
}
.panel-hd {
color: #333;
background-color: #f5f5f5;
border-bottom: 1px solid #ddd;
font-weight: bold;
cursor: pointer;
}
.panel-draggable {
position: absolute;
width: 300px;
box-shadow: 2px 2px 12px rgba(51,51,102,.3);
cursor: move;
}
</style>
</head>
<body>
<div class="panel panel-draggable js-panel1">
<div class="panel-hd">Panel1 - drag/click here</div>
<div class="panel-bd">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
<div class="panel panel-draggable js-panel2">
<div class="panel-hd">Panel2 - drag/click here(expect)</div>
<div class="panel-bd">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
<script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/jqueryui/1.11.4/jquery-ui.min.js"></script>
<!-- 为了让 jQuery UI 的组件支持移动端 touch 事件 -->
<script src="http://cdn.bootcss.com/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
<script>
$('.panel-draggable').draggable({
opacity: 0.35
});
// 这样监听会造成事件冲突, 不是我们期望的行为
$('.js-panel1 .panel-hd').on('click', function(event) {
var $target = $(event.target);
$target.parents('.panel').find('.panel-bd').slideToggle();
});
// 解决 draggable 与子元素 click 事件的冲突,
// 实现拖动时不触发子元素的 click 事件
//
// 研究发现 draggable 在拖动的时候不会触发 panel 的 click 事件
//
// 参考
// http://stackoverflow.com/questions/16578763/jquery-onclick-does-not-work-with-jquery-ui-draggable
// http://stackoverflow.com/questions/3486760/how-to-avoid-jquery-ui-draggable-from-also-triggering-click-event
$('.js-panel2').on('click', function(event) {
// 拖动的时候不会触发 click 事件
console.log('click', this);
var $target = $(event.target);
if ($target.is('.panel-hd') || $target.parents('.panel-hd').length > 0) {
$target.parents('.panel').find('.panel-bd').slideToggle();
}
});
</script>
</body>
</html>
@ufologist
Copy link
Author

解决 draggable 与子元素 click 事件的冲突, 实现拖动时不触发子元素的 click 事件

jqueryui-draggable-click.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment