2017-03-18 65 views
-1

我正在使用像Win7 Sidebar這樣的JavaScript工具製作系統,但這不適用於FILE協議。我使用的瀏覽器是Chrome。 如何讓它脫機工作?該JavaScript無法離線工作

var selected = null, // Object of the element to be moved 
 
    x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer 
 
    x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element 
 

 
// Will be called when user starts dragging an element 
 
function _drag_init(elem) { 
 
    // Store the object of the element which needs to be moved 
 
    selected = elem; 
 
    x_elem = x_pos ; 
 
    y_elem = y_pos; 
 
} 
 

 
// Will be called when user dragging an element 
 
function _move_elem(e) { 
 
    x_pos = document.all ? window.event.clientX : e.pageX; 
 
    y_pos = document.all ? window.event.clientY : e.pageY; 
 
    if (selected !== null) { 
 
     selected.style.left = (x_pos - x_elem) + 'px'; 
 
     selected.style.top = (y_pos - y_elem) + 'px'; 
 
    } 
 
} 
 

 
// Destroy the object when we are done 
 
function _destroy() { 
 
    selected = null; 
 
} 
 

 
// Bind the functions... 
 
var draggables = document.getElementsByClassName('draggable-element'); 
 
for(var i = 0; i < draggables.length; i++){ 
 
    draggables[i].onmousedown = function() { 
 
     _drag_init(this); 
 
     return false; 
 
    }; 
 
} 
 

 
document.onmousemove = _move_elem; 
 
document.onmouseup = _destroy;
body {padding:10px} 
 

 
.draggable-element { 
 
    width:125px; 
 
    height:125px; 
 
    background-color:#666; 
 
    color:white; 
 
    padding:10px 12px; 
 
    cursor:move; 
 
    position:relative; /* important (all position that's not `static`) */ 
 
}
<div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div> 
 
<div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div> 
 
<div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div> 
 
<div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div> 
 
<div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div> 
 
<div class="draggable-element">Gadget!<div style="width:20px;height:100%;background:#000;position:absolute;top:0;right:-25px"></div></div>

對於誰可以幫助,在此先感謝。

+0

你能提供你的完整的html代碼?也許那裏有些奇怪的東西。 – blackmiaool

+2

'[...]但這不起作用[...]'不是一個真正有用的信息。你在控制檯中看到任何錯誤消息嗎?什麼都沒有發生,或者它的行爲方式與預期的不同? –

+0

你說它不適用於FILE協議,這是否意味着它在HTTP上工作,或者根本不工作? – Popatop15

回答

0

與此更換_move_elem功能:

function _move_elem(e) { 
    x_pos = document.all ? window.event.clientX : e.pageX; 
    y_pos = document.all ? window.event.clientY : e.pageY; 
    if (selected !== null) { 
     selected.style.left = (x_pos) + 'px'; 
     selected.style.top = (y_pos) + 'px'; 
    } 
} 

我想減去位置是把它扔了,把它回到了起點。我也使用文件uri,並且它可以工作。希望這可以幫助! 編輯:Codepen在http://codepen.io/JohnJ1101/pen/oZpwGv

+0

請分享所有javascript。我完全無法理解它。 – Plasmmer

+0

http://codepen.io/JohnJ1101/pen/oZpwGv – JohnJ1101

+0

謝謝。但它在這裏仍然不起作用。 – Plasmmer