前端實(shí)踐-便簽系統(tǒng)(上)

這個(gè)便簽案例是學(xué)習(xí)網(wǎng)易云課堂上的便簽系統(tǒng),自己重新整理,以便后期深入學(xué)習(xí)。

1、布局

<button id="create">創(chuàng)建</button>
    <div class="note">
        <i class="close"></i>
        <div class="editor" contenteditable="true"></div>
        <div class="timestamp">
            <span>更新</span>
            <span class="time">2017-07-01</span>
        </div>
    </div>
body {
    font-size: 14px;
    font-family: "微軟雅黑";
}
.note {
    position: absolute;
    width: 200px;
    height: 300px;
    box-shadow:  0 5px 10px rgba(0,0,0,0.5);
    background: #FFE73E;
    padding: 10px;
}
.note .close {
    position: absolute;
    width: 30px;
    height: 30px;
    top: -15px;
    left: -15px;
    background: url(img/deleteButton.png) no-repeat;
    display: none;
}
.note:hover .close {
    display: block;
} 
.note .editor {
    position: absolute;
    bottom: 30px;
    left: 10px;
    top: 10px;
    right: 10px;
    outline: none;
    overflow: auto;
}
.note .timestamp {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 0 10px;
    height: 30px;
    line-height: 30px;
    background: #db0;
    color: #fff;
    font-size: 12px;
}

2、創(chuàng)建

布局在js文件里面用ES6的模塊字符串寫(xiě)入,代碼如下:

  var noteTpl = `   
        <i class="close"></i>
        <div class="editor" contenteditable="true"></div>
        <div class="timestamp">
            <span>更新</span>
            <span class="time">2017-07-01</span>
        </div>
    `;
    function createNote ( options ){
        var note = document.createElement('div');
        note.className = 'note';
        note.innerHTML = noteTpl;       
        document.body.appendChild(note);
        this.note=note;
        this.addEvent();
    };
    document.addEventListener( 'DOMContentLoaded', function(e){
        //創(chuàng)建添加事件
        $('#create').addEventListener('click',function(e){
            new createNote();
        })
    });

3、刪除


  createNote.prototype.close = function (e){        
        document.body.removeChild(this.note);
    };
  createNote.prototype.addEvent = function (){
    //關(guān)閉處理程序,關(guān)閉監(jiān)聽(tīng)事件
    var closeHandler = function (e){
      this.close(e);
      $('.close',this.note).removeEventListener('click',closeHandler);
    }.bind(this);
    $('.close',this.note).addEventListener('click',closeHandler);
  };

4、移動(dòng)

主要是在當(dāng)前的note下添加 mousedown(記錄X、Y坐標(biāo))、mousemove、mouseup事件

定義全局變量
var moveNote = null;//被移動(dòng)的note
var startX ;//X坐標(biāo)
var startY ;//Y坐標(biāo)
var maxZIndex = 0;//z-index值
創(chuàng)建時(shí),也要?jiǎng)?chuàng)建標(biāo)簽的left值和top值
function createNote ( options ){
        var note = document.createElement('div');
        note.className = 'note';
        note.innerHTML = noteTpl;
        note.style.left = options.left + 'px';
        note.style.top = options.top + 'px';
        note.style.zIndex = options.zIndex;
        document.body.appendChild(note);
        this.note=note;     
        this.addEvent();
    };
$('#create').addEventListener('click',function(e){
  new createNote({
    left:Math.round(Math.random()*(window.innerWidth - 220)),
    top:Math.round(Math.random()*(window.innerHeight - 320)),
    zIndex: maxZIndex++,
  });
})
添加mousedown事件
var mousedownHandler = function (e){
  moveNote = this.note;
  startX = e.clientX - this.note.offsetLeft;
  startY = e.clientY - this.note.offsetTop;
  if(parseInt(this.note.style.zIndex)!==maxZIndex - 1){
    this.note.style.zIndex = maxZIndex++;
    }
  }.bind(this);
this.note.addEventListener('mousedown' , mousedownHandler);

添加mousemove事件

//移動(dòng)監(jiān)聽(tīng)
var mousemoveHandler = function (e){
  if(!moveNote){
    return;
  }
  moveNote.style.left = e.clientX - startX + 'px';
  moveNote.style.top = e.clientY - startY + 'px';  
}
document.addEventListener('mousemove', mousemoveHandler);

添加mouseup事件

var mouseupHandler = function (e){
  moveNote = null;
}
document.addEventListener('mouseup', mouseupHandler);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • (續(xù)jQuery基礎(chǔ)(1)) 第5章 DOM節(jié)點(diǎn)的復(fù)制與替換 (1)DOM拷貝clone() 克隆節(jié)點(diǎn)是DOM的常...
    凜0_0閱讀 1,518評(píng)論 0 8
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,120評(píng)論 25 708
  • 風(fēng)雪刮不倒 敵人打不倒 我們學(xué)習(xí) 我們前進(jìn)
    薔薇颯閱讀 246評(píng)論 0 0
  • 今天剛在朋友圈中,發(fā)現(xiàn)了這個(gè)可以宣泄內(nèi)心的想法的軟件,這里沒(méi)有紛爭(zhēng)沒(méi)有什么所謂的虛偽和勾心斗角…… 不知道從什么時(shí)...
    丑小鴨的書(shū)閱讀 247評(píng)論 0 0
  • 我記得我家以前在向陽(yáng)巷
    思緒的速度閱讀 344評(píng)論 0 0

友情鏈接更多精彩內(nèi)容