2016-06-18 4 views
-1

私は歌詞アップロードのために以下のフォームを持っています。私はフォームのデザインを少し変えましたが、今は奇妙な問題に直面しています。"float:left" divは入力フォーカスでプッシュダウンされます

JSを使用して偽のデータリストを作成しました。入力フォーカスでは、偽データリスト(ul要素)が入力要素の隣に追加されます。その位置は絶対に設定されているため、文書が現れたときに文書の流れを乱してはいけません。しかし、そうです。私は問題を特定しているように見えません。データリストが表示されると、テーブルの隣のdivがプッシュダウンされます。テーブルの幅はデータリストが表示されても変更されないので、divを二重にして押し下げるわけではありません。 #songInputからoverflow: auto;、テーブルの親要素とDIVを取り外し

Code Pen

var artists = [{"artist":"3 Doors Down"},{"artist":"5 Seconds of Summer"},{"artist":"Adele"},{"artist":"Alicia Keys"},{"artist":"Amanda Abizaid"},{"artist":"Avril Lavigne"}]; 
 
var albums = [{"album":"The Better Life","year":"2000","cover":"3_doors_down_2000_the_better_life.jpg"},{"album":"Away from the Sun","year":"2002","cover":"3_doors_down_2002_away_from_the_sun.jpg"},{"album":"Seventeen Days","year":"2005","cover":"3_doors_down_2005_seventeen_days.jpg"},{"album":"3 Doors Down","year":"2008","cover":"3_doors_down_2008_3_doors_down.jpg"},{"album":"Time of My Life","year":"2011","cover":"3_doors_down_2011_time_of_my_life.jpg"}]; 
 
var songs = [{"song":"Kryptonite","track_no":"1"},{"song":"Duck and Run","track_no":"3"},{"song":"Be Like That","track_no":"5"},{"song":"So I Need You","track_no":"11"}]; 
 

 
function datalist(element) { 
 
    return new datalist.prototype.init(element); 
 
} 
 
datalist.prototype = { 
 
    init: function(element) { 
 
    if (!element) { 
 
     this.element = document.createElement("ul"); 
 
     this.element.classList.add("datalist");; 
 
     this.hide(); 
 
    } else { 
 
     this.element = element; 
 
    } 
 
    }, 
 
    update: function(queryElement) { 
 
    this.clear(); 
 
    var lookUpArray = queryElement.name + "s"; 
 
    var results = this.search(window[lookUpArray], queryElement.value, queryElement.name); 
 
    for (var i = 0; i < results.length; i++) { 
 
     var li = document.createElement("li"); 
 
     var value = results[i][queryElement.name]; 
 
     switch (queryElement.name) { 
 
     case "album": 
 
      li.setAttribute("data-year", results[i].year); 
 
      break; 
 
     case "song": 
 
      li.setAttribute("data-track_no", results[i].track_no); 
 
      break; 
 
     } 
 
     if (queryElement.value != "") { 
 
     var re = new RegExp(queryElement.value, "gi"); 
 
     value = value.replace(re, "<span class=\"highlight\">" + "$&" + "</span>"); 
 
     } 
 
     li.innerHTML = value; 
 
     this.element.appendChild(li); 
 
    } 
 
    return results.length; 
 
    }, 
 
    search: function(lookUpArray, string, queryType) { 
 
    var results = []; 
 
    for (var i = 0; i < lookUpArray.length; i++) { 
 
     if (lookUpArray[i][queryType].toLowerCase().search(string.toLowerCase()) != -1) { 
 
     results.push(lookUpArray[i]); 
 
     } 
 
    } 
 
    return results; 
 
    }, 
 
    clear: function() { 
 
    this.element.innerHTML = ""; 
 
    }, 
 
    hide: function() { 
 
    this.element.style.display = "none"; 
 
    }, 
 
    show: function() { 
 
    this.element.style.display = ""; 
 
    }, 
 
    remove: function() { 
 
    this.element.parentElement.removeChild(this.element); 
 
    }, 
 
    for: function(sibling) { 
 
    sibling.parentElement.appendChild(this.element); 
 
    this.hide(); 
 
    }, 
 
}; 
 
datalist.prototype.init.prototype = datalist.prototype; 
 

 
var lastVisitedInput = null; 
 

 
$("#lyrics-form").on("focus", "input.datalist-input", function() { 
 
    if (this.parentElement.children.length == 1) { 
 
    this.parentElement.appendChild(datalist().element); 
 
    } 
 
    if (lastVisitedInput) { 
 
    datalist(lastVisitedInput.nextElementSibling).hide(); 
 
    } 
 
    lastVisitedInput = this; 
 
    if (datalist(this.nextElementSibling).update(this)) { 
 
    datalist(this.nextElementSibling).show(); 
 
    } else { 
 
    datalist(this.nextElementSibling).hide(); 
 
    } 
 
}); 
 

 
$(document).on("click", function(e) { 
 
    if (lastVisitedInput) { 
 
    var exceptions = getExceptions(lastVisitedInput); 
 
    if (!contains(exceptions, e.target)) { 
 
     datalist(lastVisitedInput.nextElementSibling).remove(); 
 
     lastVisitedInput = null; 
 
    } 
 
    } 
 
}); 
 

 
$("#lyrics-form").on("input", "input.datalist-input", function() { 
 
    if (datalist(this.nextElementSibling).update(this)) { 
 
    datalist(this.nextElementSibling).show(); 
 
    } else { 
 
    datalist(this.nextElementSibling).hide(); 
 
    } 
 
}); 
 

 
$("#lyrics-form").on("click", "li", function() { 
 
    this.parentElement.previousElementSibling.value = this.innerText; 
 
    $(this.parentElement.previousElementSibling).trigger("input"); 
 
}); 
 

 
function getRecord(input) { 
 
    var lookUpArray = window[input.name + "s"]; 
 
    for (var i = 0; i < lookUpArray.length; i++) { 
 
    if (input.value == lookUpArray[i][input.name]) { 
 
     return lookUpArray[i]; 
 
    } 
 
    } 
 
    return false; 
 
} 
 

 
function getExceptions(input) { 
 
    var exceptions = [ 
 
    input, 
 
    input.nextElementSibling, 
 
    ]; 
 
    for (var i = 0; i < input.nextElementSibling.children.length; i++) { 
 
    exceptions.push(input.nextElementSibling.children[i]); 
 
    } 
 
    return exceptions; 
 
} 
 

 
function contains(array, item) { 
 
    for (var i = 0; i < array.length; i++) { 
 
    if (array[i] === item) { 
 
     return true; 
 
    } 
 
    } 
 
    return false; 
 
}
* { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } *, *:before, *:after { box-sizing: inherit; } body { line-height: 1.5; font-family: sans-serif; } input[type="button"], input[type="submit"] { cursor: pointer; } textarea, input[type="text"], input[type="search"], input[type="number"], input[type="password"] { border: 1px solid rgba(0,0,0,.2); padding: 4px; margin: 1px; } table { border-collapse: collapse; border-spacing: 0; } 
 
\t \t \t 
 
body { 
 
    background-color: rgb(230, 230, 230); 
 
    font-family: Arial, sans-serif; 
 
    font-size: 14px; 
 
    color: rgba(0, 0, 0, .8); 
 
    box-sizing: border-box; 
 
} 
 

 
#main { 
 
    height: 500px; 
 
    background: white; 
 
    box-shadow: 0 0 2px rgba(0, 0, 0, .1), 0 2px 2px rgba(0, 0, 0, .1); 
 
    margin: 20px auto; 
 
    display: table; 
 
    padding: 20px; 
 
} 
 

 
#songInput { 
 
    overflow: auto; 
 
} 
 

 
#songTable td { 
 
    position: relative; 
 
} 
 

 
#songTable, 
 
#coverDiv { 
 
    float: left; 
 
} 
 

 
#coverDiv { 
 
    margin-left: 20px; 
 
} 
 

 
#artist, 
 
#album, 
 
#song { 
 
    width: 250px; 
 
} 
 

 
#artist { 
 
    width: 300px; 
 
    width: 100%; 
 
} 
 

 
#year, 
 
#track_no { 
 
    width: 70px; 
 
} 
 

 
#songTable td { 
 
    padding-bottom: 20px; 
 
} 
 

 
#songTable td:first-child { 
 
    padding-right: 10px; 
 
} 
 

 
#songTable .int-input { 
 
    padding-left: 20px; 
 
    padding-right: 10px; 
 
} 
 

 
#coverDiv > * { 
 
    display: block; 
 
} 
 

 
#coverDiv img { 
 
    width: 137px; 
 
    height: 137px; 
 
    border: 1px solid rgba(0, 0, 0, .2); 
 
    margin: 1px; 
 
} 
 

 
#coverUpload { 
 
    margin: 1px; 
 
    margin-top: 10px; 
 
    width: 250px; 
 
} 
 

 
#lyricsBox { 
 
    width: 100%; 
 
    height: 400px; 
 
    margin-top: 15px; 
 
} 
 

 
#submit { 
 
    width: 100%; 
 
    margin-top: 15px; 
 
} 
 

 
.datalist { 
 
    border: 1px solid silver; 
 
    box-shadow: 0 2px 5px rgba(0, 0, 0, .5); 
 
    position: absolute; 
 
    top: 32px; 
 
    left: 1px; 
 
    background: white; 
 
    padding: 5px; 
 
    max-height: 195px; 
 
    width: 180px; 
 
    width: 100%; 
 
    overflow-y: scroll; 
 
    z-index: 1000; 
 
} 
 

 
.datalist li { 
 
    padding: 2px 5px; 
 
    cursor: default; 
 
} 
 

 
.datalist li:hover { 
 
    background: rgba(0, 0, 0, .05); 
 
    color: black; 
 
} 
 

 
.datalist .highlight { 
 
    font-weight: bold; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="main"> 
 
    <form action="addlyrics.php" id="lyrics-form" method="post" autocomplete="off" enctype="multipart/form-data"> 
 
    <div id="songInput"> 
 
     <table id="songTable"> 
 
     <tr> 
 
      <td>Artist</td> 
 
      <td colspan="3"> 
 
      <input type="search" name="artist" id="artist" class="datalist-input" placeholder="Artist" required /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Album</td> 
 
      <td> 
 
      <input type="search" name="album" id="album" class="datalist-input" placeholder="Album" required /> 
 
      </td> 
 
      <td class="int-input">Year</td> 
 
      <td> 
 
      <input type="number" name="year" id="year" class="input-num" placeholder="Year" required /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Song</td> 
 
      <td> 
 
      <input type="search" name="song" id="song" class="datalist-input" placeholder="Name" required /> 
 
      </td> 
 
      <td class="int-input">#</td> 
 
      <td> 
 
      <input type="number" name="track_no" id="track_no" class="input-num" placeholder="ID" required /> 
 
      </td> 
 
     </tr> 
 
     </table> 
 
     <div id="coverDiv"> 
 
     <img src="covers/blank.gif" id="cover" /> 
 
     <input type="file" name="cover" id="coverUpload" accept="image/*" /> 
 
     </div> 
 
    </div> 
 
    <textarea name="lyrics" placeholder="Lyrics" id="lyricsBox" /></textarea> 
 
    <input type="submit" id="submit" class="button" /> 
 
    </form> 
 
</div>

+0

検索の代わりにがあると、カバーが左に移動せず、下にドロップダウンの問題があります。また、class = "datalist-input" "データリスト入力"?私はそれを見ることができません私は唯一参照してください.datalist – Grisza

+0

@グリッサ入力要素は '.datalist-input'を持っています。擬似データリスト( 'ul.datalist')はプログラムによって追加されました。 – akinuri

答えて

0

、問題を解決しました。しかし、私は親の上のoverflow: auto;がdivをプッシュする理由を理解していません。動的に追加されたul.datalistの位置は絶対に設定され、表示されるときは、テーブルの高さを延長するだけです(右側にdivを表示しないでください)。

関連する問題