如何在可拖动元素中使用%代替px
How to use % instead of px with draggable element?
我被卡住了,我需要帮助。我正在用记号笔画地图。我可以添加标记等等。我的主容器有100%的宽度和高度。当我点击某个地方时,我的标记有%值,例如top: 34%;左:35%;拖动标记后,新位置有px值。我想保存%的值。什么好主意吗?
map.js
jQuery(document).ready(function($){
// basic add
$("button#remove").click(function(){
$(".marker").remove();
});
//add with position
var map = $(".map");
var pid = 0;
function AddPoint(x, y, maps) {
// $(maps).append('<div class="marker"></div>');
var marker = $('<div class="marker ui-widget-content"></div>');
marker.css({
"left": x,
"top": y
});
marker.attr("id", "point-" + pid++);
$(maps).append(marker)
$('.marker').draggable().resizable();
}
map.click(function (e) {
// var x = e.pageX - this.offsetLeft;
// var y = e.pageY - this.offsetTop;
var x = e.offsetX/ $(this).width() * 100 + '%';
var y = e.offsetY/ $(this).height() * 100 + '%';
// $(this).append('<div class="marker"></div>');
AddPoint(x, y, this);
});
// drag function
$(".ui-widget-content").draggable({
drag: function( event, ui ) {
var x = e.offsetX/ $(this).width() * 100 + '%';
var y = e.offsetY/ $(this).height() * 100 + '%';
}
});
});
style.css
.wrapper {
margin: 0 auto;
width: 100%;
min-height: 400px;
overflow: hidden;
}
.map {
width: 100%;
position: relative;
}
.map > img {
max-width: 100%;
max-height: 100%;
}
.marker {
width: 10px;
height: 10px;
border-radius: 50%;
background: rgba(255, 0, 0, 1);
position: absolute;
left: 50%;
top: 50%;
z-index: 999;
}
#mapa {
width: 30px;
height: 30px;
border-radius: 50%;
background: rgba(255, 0, 0, 1);
z-index: 999;
}
一些HTML代码
<div class="wrapper">
<div class="map">
<img src="http://i.imgur.com/HtxXGR5.jpg" width="100%" height="auto" margin="15px 0;" alt="">
</div>
<button id="remove">Remove all markers</button>
</div>
如果你想捕获位置,在拖动结束后(每次拖动5次),你可以这样做:
$('.marker').draggable({
stop: function() {
var offset = $(this).offset();
var mapOffest = $('.map').offset();
var x = ((offset.left - mapOffest.left)/ ($(".map").width() / 100))+"%";
var y = ((offset.top - mapOffest.top)/ ($(".map").height() / 100))+"%";
// We need to do something with thoses vals uh?
$(this).css('left', x);
$(this).css('top', y);
// or
console.log('X:'+x + '||Y:'+y);
}
});
这里小提琴
请注意,事件是直接在标记上设置的,这可能是您的错误。
这个小计算需要考虑到地图偏移量,如果你的地图是全宽/全高(窗口大小),你就不需要了
如果您需要或更喜欢使用drag
事件而不是stop
,这些行将不会产生任何影响
$(this).css('left', x);
$(this).css('top', y);
但是您仍然可以捕获位置,控制台值将是货物。
问题解决了,再次感谢DFayet最终代码
jQuery(document).ready(function($){
// basic add
$("button#remove").click(function(){
$(".marker").remove();
});
//add with position
var map = $(".map");
var pid = 0;
function AddPoint(x, y, maps) {
var marker = $('<div class="marker"></div>');
marker.css({
"left": x,
"top": y
});
marker.attr("id", "point-" + pid++);
$(maps).append(marker);
$('.marker').draggable({
stop: function(event, ui) {
var thisNew = $(this);
var x = (ui.position.left / thisNew.parent().width()) * 100 + '%';
var y = (ui.position.top / thisNew.parent().height()) * 100 + '%';
thisNew.css('left', x);
thisNew.css('top', y);
console.log(x, y);
}
});
}
map.click(function (e) {
var x = e.offsetX/ $(this).width() * 100 + '%';
var y = e.offsetY/ $(this).height() * 100 + '%';
AddPoint(x, y, this);
});
});
相关文章:
- 如果宽度是百分比,如何设置以px为单位的图像高度
- 为什么有些jQuery程序员使用像$.get、$.getJSON和$.when这样的jQuery函数来代替$.ajax
- 创建一个新元素来代替现有元素
- jquery匹配以px为单位的样式字体大小的元素
- Bootstrap DateTime选取器--如何将“;00”;代替秒
- jqplot.replot无法正常工作.图例仅由两个标签代替:“;1〃;以及“;2〃;
- 同构JS应用程序中的环境变量:Webpackfind&代替
- 如何在Kendo UI Scheduler中添加Resource作为文本框来代替Dropdown
- 在.dotddot脚本中将高度值从px更改为%
- jquery$(“#someID”).width(变量+'px')有效,现在无效
- CSS”;边界宽度“;(px)属性错误
- 如何用JavaScript中的Ajax调用代替加载脚本文件(将YQL与JavaScript一起使用)
- 有没有办法在Ionic/Angular/Cordova堆栈中找到以dp(而不是px)表示的屏幕大小
- 通过“;类“"实例”;使用ReactJS组件代替“props”文字
- 使用javascript代替Iframe源
- 如何用[b]代替<b>使用javascript或jquery
- 如何在图像源中使用字符串代替url
- 在字符串中插入下划线以代替单个或多个空格(连续)
- 在javascript计算-D3图表中使用%代替px
- 如何在可拖动元素中使用%代替px