2017-12-21 21 views
0

私は自分のプロジェクトに悩まされています。 以下のコードには、javascriptとcssのみを使用してフェードインとフェードアウトの効果を追加する必要があります。私を助けてください。 すべてのデータをローカルストレージに保存するためにこの関数を作成しました。保存ボタンをクリックすると、フェードインでノートを追加します。私がノートを削除しているときに、それをフェードアウトで削除したいと思っています。私は追加または削除しています唯一のノートでは、これはメモを追加する機能です効果フェードインとフェードアウトを追加する方法

で追加または削除されます。

function saveIt() { 
 
    var content = document.getElementById("content").value; 
 
    var date = document.getElementById("date").value; 
 
    var time = document.getElementById("time").value; 
 
    var id = Math.floor(Math.random() * 100000); //creating random id number. 
 

 
    var note = { id: id, content: content, date: date, time: time }; 
 
    notesArray.push(note); 
 

 
    localStorage.myList = JSON.stringify(notesArray); 
 
    notesNewArray = JSON.parse(localStorage.myList); 
 

 
    for (var i = 0; i < notesNewArray.length; i++) { 
 
     theId = notesArray[i].id; 
 
     var output = "<div id='justFade'>" + "<div class='main col-xm-12 col-sm-6 col-md-4 col-lg-3'>" + "<div class='note-bg'>" + "<div id='" + theId + "'" + "onclick='deleteNote(this.id)'>" + "<p id='hide-delete' class='glyphicon glyphicon-remove-circle'></p>" + "</div>" + "<div class='noteContent'>" + notesNewArray[i].content + "</div>" + "<div class='noteDate'>" + notesNewArray[i].date + "</div>" + "<div>" + notesNewArray[i].time + "</div>" + "</div>" + "</div>" + "</div>";   
 
    } 
 

 
    document.getElementById("results").innerHTML += output; 
 
    
 
    
 
    document.getElementById("msg").innerHTML = ""; //to hide the message for the empty note. 
 
    document.getElementById("msgDate").innerHTML = ""; //to hide the message for the empty or wrong date. 
 
    document.getElementById("msgTime").innerHTML = ""; //to hide the message for the empty or wrong date. 
 
    document.forms['myNotes'].reset(); //reseting the form on saving. 
 
    
 
    
 
    function deleteNote(clickedId) { 
 
    var myList = localStorage.myList; 
 
    notesArray = JSON.parse(myList); 
 
    for (var i = 0; i < notesArray.length; i++) { 
 
     if (notesArray[i].id == clickedId) { 
 
      notesArray.splice(i, 1); // searching for the same ID number that the user clicked on and deleting it. 
 
     } 
 
     localStorage.myList = JSON.stringify(notesArray); 
 
     notesNewArray = JSON.parse(localStorage.myList);   
 

 
     document.getElementById("results").innerHTML = ""; 
 
     getIt(); // calling again to the notes from the local storage.  
 
    } 
 
}
<!DOCTYPE html> 
 

 
<html lang="en"> 
 
    <head> 
 
     <meta charset="utf-8" /> 
 
     <title>My Project</title> 
 
     
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
     <link rel="stylesheet" type="text/css" href="css/styles.css"> 
 
     <link href="https://fonts.googleapis.com/css?family=Indie+Flower" rel="stylesheet"> 
 
     
 

 
     
 

 
    </head> 
 
    <body id="init"> 
 

 
     <div class="center"> 
 
      <img src="img/title.png" class="img-center" alt="My Task Board" width="876" height="225">   
 
     </div> 
 

 
     <div class="container-fluid">    
 
      <div class="row"> 
 
       <div class="col-sm-4"></div> 
 
       <div class="col-sm-4"> 
 
        <form id="myNotes" name="myNotes"> 
 
         <div class="form-group"> 
 
          <textarea rows="8" name="myContent" cols="100" class="form-control" id="content" placeholder="type your note here..." required></textarea><br>        
 
          <div><input type="date" class="form-control" id="date" required></div> 
 
          <div><input type="time" class="form-control" id="time" required></div> 
 
          <input type="submit" class="btn btn-primary" id="save" value="save"> 
 
          <div id="msg"></div> 
 
          <div id="msgDate"></div> 
 
          <div id="msgTime"></div> 
 
         </div>          
 
        </form> 
 
       </div>      
 
       <div class="col-sm-4"></div> 
 
      </div> 
 
     </div> 
 
     <div id="results" class="singleNote"></div> 
 
     <script src="js/myscript.js"></script> 
 
    </body> 
 
</html>

はあなたに感謝。

+2

あなたのHTMLマークアップは、単に 'fadeIn()'と 'fadeOut()'を使うのではないのですか? –

+0

こんにちは、私はjQueryを使用していません....あなたは私がJSのみを使用して解決してください助けることができますか? –

+0

@IdanDamti下の私の答えを確認してください。 'plain javascript'を使って' fade in'と 'fade out'の動作例を示します。 – Ivan86

答えて

0

jqueryを使用すると、この問題を解決できます。あなたがこれを行うことができますフェードインまたはフェードアウトするために必要なすべての要素については...ここで

$("#div1").fadeIn(); 
$("#div1").fadeOut(); 

は説明するリンクです... Jquery fade in and out

+0

こんにちは、私はjQueryを使用することはできません...あなたはJSだけでそれをやってくれますか? –

0

次の2つのasync機能fadeIn()と​​をする必要があるとpromiseを返す関数。

を使用すると、他のコードが実行を継続できることを意味する関数asynchronouslyを解決しています。実行を開始するために以前に呼び出された関数が返されるのを待つ必要はありません。このため、4つの異なるコード行によって呼び出されても、同時にフェードイン/フェードするように見えます。ここで

fade in、プレーンJavaScriptで行わfade outの簡単な例です: あなたは既にに従ってjQueryのを使用している(コードの下部にスニペットを実行します)

function sleep(ms) 
 
{ 
 
    return new Promise(resolve => setTimeout(resolve, ms)); 
 
} 
 

 
async function fadeIn(elmnt) 
 
{ 
 
    for(var i=0; i<101; i++) 
 
    { 
 
    document.getElementById(elmnt).style.opacity = i/100; 
 
    await sleep(20);  // Play with the sleep time to suit your needs, in miliseconds 
 
    } 
 
} 
 

 

 
async function fadeOut(elmnt) 
 
{ 
 
    for(var i=100; i > -1; i--) 
 
    { 
 
    document.getElementById(elmnt).style.opacity = i/100; 
 
    await sleep(20); 
 
    } 
 
} 
 

 
// Simply call the functions anywhere you want like this 
 
fadeIn('fade_1'); 
 
fadeIn('fade_2'); 
 

 
fadeOut('fade_3'); 
 
fadeOut('fade_4');
.opacityZero 
 
{ 
 
    opacity:0; 
 
} 
 

 

 
.opacityOne 
 
{ 
 
    opacity:1; 
 
} 
 

 
.firstDiv 
 
{ 
 
    position:relative; 
 
    float:left; 
 
    width:97%; 
 
    border:1px solid #000; 
 
    padding:5px; 
 
} 
 

 
.secondDiv 
 
{ 
 
    position:relative; 
 
    float:left; 
 
    width:98%; 
 
    border:1px solid #000; 
 
    padding:5px; 
 
} 
 

 
.container 
 
{ 
 
    position:relative; 
 
    float:left; 
 
    width:95%; 
 
    margin-top:20px; 
 
    border:1px solid #09f; 
 
    padding:10px; 
 
} 
 

 
h3 
 
{ 
 
    font-family:calibri; 
 
    position:relative; 
 
    float:left; 
 
}
<h3>Fade in effect</h3> 
 

 
<div id="fade_1" class="firstDiv opacityZero">hi</div> 
 

 
<div class="container"> 
 
    <span id="fade_2" class="secondDiv opacityZero">hi</span> 
 
</div> 
 

 
<h3 style="margin:20px 0 0;">Fade out effect</h3> 
 

 
<div id="fade_3" class="firstDiv opacityOne">hi</div> 
 

 
<div class="container"> 
 
    <span id="fade_4" class="secondDiv opacityOne">hi</span> 
 
</div>

関連する問題