我是JavaScript新手,我想以某種方式「掌握」它,爲了讓我這樣做,我不想使用任何庫[如jQuery]。我怎樣才能「用簡單的JavaScript滑動鼠標」?
話雖這麼說,我已經找到了幾種方法來實現「鼠標刷卡」只有JavaScript的,如:Simulate swipe with mouse in javascript。 但是我發現的帖子中的答案總是很淺。即使所有的解決方案都指向:mousedown - > mousemove - > mouseup事件。這是我正在做的事情。
我爲自己設置了一個練習來「掌握」javascript的基礎:我將不得不創建一個平面和跨瀏覽器兼容的界面,它允許我保存筆記(在本地存儲中)僅使用javascript,css3和html5(最難的部分是跨瀏覽器兼容性)。
所以我想到了一個簡單的2列表:保存筆記(第一列)和筆記(第二列)的標題。即
FYI:我想張貼的圖片,但似乎我不是計算器人氣不足:「你至少需要10聲譽發表圖片」,所以我把它像一個正常的鏈接,我希望它是好的。
最重要的是表格。我希望能夠「滑動」表格的行。
這是我已經拿出來(我不會把所有的代碼中,我得到了什麼只是基礎知識):
第一:該表是一組通過CSS類;
/*CSS*/
div.table {
display: table;
}
div.table .row{
display: table-row;
}
div.table .row .col, div.table .row .col-2{
display: table-cell;
}
二:html的;
<div id="savedNotesTable" class="table">
<div class="row">
<div class="col">Testing</div>
<div class="col-2">This is a test</div>
</div>
<div class="row">
<div class="col">Testing</div>
<div class="col-2">This is a test</div>
</div>
<div class="row">
<div class="col">Testing</div>
<div class="col-2">This is a test</div>
</div>
</div>
最後但並非最不重要的:javascript的
function addEvent(element, type, fn){
// custom add event function that cares about compatibility
// returns fn
}
function removeEvent(element, type, fn){ /*custom remove event function that cares about compatibility and removes only the specific handler*/ }
function addSwipe(element, parent){
addEvent(element, "mousedown", function(e){
var mouseupHandler = addEvent(document, "mouseup", mouseUpHandler);
var mousemoveHandler = addEvent(document, "mousemove", function(e){
// element.style.position = "static";
// parent.style.left = e.pageX + 'px';
// add transition and blur to the element
// mouseSwipe = absolute value of(originalPosition of mouse - e.pageX)
});
function mouseUpHandler(e){
// pseudocode:
// if mouseSwipe >= 50%(div size) = delete from the parent which is the "table"
// transition and blur OUT to 100% and delete
// if mouseSwipe < 50%(div size) = don't delete
// transition back to 0% and unblur.
removeEvent(document, "mousemove", mousemoveHandler);
removeEvent(document, "mouseup", mouseupHandler);
};
});
};
// this is just to add them to the test notes on the html
// The real thing is going to add the swipe when I save the new element
var table = document.getElementById("savedNotesTable");
var notes = table ? table.childNodes : undefined;
for(var prop in notes){
if(prop != "length" && notes.hasOwnProperty(prop)){
if(notes[prop].nodeName != "#text"){
addSwipe(notes[prop], table);
};
};
};
我這樣做對嗎?還是有另一種方式我沒有看到? 我能夠在onmousedown事件中弄到某處像位置=「絕對」那樣的div位置,但這與表格行的寬度和長度混淆了。
我正在尋找最佳實踐。
您可能感興趣的http://codereview.stackexchange.com/。我閱讀這篇文章尋找一個錯誤,但似乎你正在尋找改進或不正確的代碼。 stackoverflow.com更多的是要求如何解決問題。 codereview.stackexchange.com更適合..代碼評論。如果你有一個特定的錯誤,你需要幫助,讓我知道,但我不會閱讀整個項目尋找修復,除非我在codereviews。 – DutGRIFF
獲取jQuery並使用'.on(「swipe」,function(){});' – Cilan
@ManofSnow他的更新狀態他不想使用庫...他專門命名爲jQuery。 :) – DutGRIFF