2017-12-20 9 views
0

私は内部にいくつかの要素があるセクションがあります。彼らはすべて同じ行/行にあります。 div要素とCSSを組み合わせて、すべての要素を隣り合わせに配置しました。要素をレイアウトするのがベストプラクティスかどうかは分かりません。私の問題は、ユーザーが検索基準を満たしていない場合に発生するメッセージボックスです。フィールドボックスはdisplay:noneに設定されています。メッセージが表示されたらSearchをクリックすると、すべてが左に移動します。要素を左に押すのではなく、画面の右側にメッセージを展開できるかどうか疑問に思っていますか?また、私はそれがどのように小さな画面に影響し、その場合に最良の選択肢になるのだろうかと思います。divを設定してスペースをコンテナの最後に置くにはどうすればいいですか?

$('#searchBtn').on('click',searchFun); 
 

 
function searchFun(){ 
 
    var menuVal = $.trim($('#menu').val()), 
 
\t searchFldVal = $.trim($('#searchFld').val()); 
 
    
 
    if(!searchFldVal){ 
 
\t \t $('#searchMsg').addClass("error").show().text('This field is required.').delay(4000).fadeOut('slow').queue(function(){ 
 
\t \t  $(this).removeClass("error").dequeue(); 
 
\t \t }); 
 
\t }else if(searchFldVal.length < 3){ 
 
\t \t $('#searchMsg').addClass("error").show().text('You must enter at least 3 characters to search on.').delay(4000).fadeOut('slow').queue(function(){ 
 
\t \t  $(this).removeClass("error").dequeue(); 
 
\t \t }); 
 
\t }else{ 
 
    console.log('Search record.'); 
 
    } 
 
}
section.mainBox{ 
 
\t width: 100%; 
 
\t padding-top: 0; 
 
\t background-color: white; 
 
\t border: 2px solid #000099; 
 
} 
 
div#Container { 
 
\t padding-top: 8px; 
 
\t padding-bottom: 8px; 
 
\t text-align: center; 
 
\t background-color: #B0C4DE; 
 
\t border-bottom: 2px solid #000099; 
 
} 
 
div.nextTo { 
 
\t display: inline-block; 
 
\t margin-left: 5px; 
 
} 
 
span.msgMainBox { 
 
\t display:none; 
 
\t margin: 2px 5px; 
 
\t padding:2px 25px 2px 35px; 
 
} 
 
span.error { 
 
\t border: 1px solid; 
 
\t margin: 5px; 
 
\t padding:5px 10px 5px 40px; 
 
\t background-repeat: no-repeat; 
 
\t background-position: 10px center; 
 
\t border-radius: 3px; 
 
\t display: block; 
 
} 
 
span.error { 
 
\t color: #D8000C; 
 
\t background-color: #FFBABA; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class="mainBox"> 
 
    <div id="Container"> 
 
    <div class="nextTo"> 
 
     <select name="menu" id="menu"> 
 
     <option value="1">Name</option> 
 
     <option value="2">DOB</option> 
 
     <option value="3">City</option> 
 
     </select> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="text" name="searchFld" id="searchFld" size="24" maxlength="24" value="" title="Maximum size of the field is 24 characters." placeholder="Example: Marco Polo" /> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="button" name="searchBtn" id="searchBtn" value="Search"/> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="button" name="lockBtn" id="lockBtn" value="Unlock" style="display:none" /> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <span id="searchMsg" class="msgMainBox"></span> 
 
    </div> 
 
    </div> 
 
</section>

+0

入力タグの周りのdivの目的は何ですか?また、 ''は、閉じスラッシュを使用しないか、必要としません。 – Rob

答えて

1

あなたはプッシュ効果を避けるための要素の絶対位置を作ることができます。そして、小さな画面のためにそれを下に移動します:

$('#searchBtn').on('click', searchFun); 
 

 
function searchFun() { 
 
    var menuVal = $.trim($('#menu').val()), 
 
    searchFldVal = $.trim($('#searchFld').val()); 
 

 
    if (!searchFldVal) { 
 
    $('#searchMsg').addClass("error").show().text('This field is required.').delay(4000).fadeOut('slow').queue(function() { 
 
     $(this).removeClass("error").dequeue(); 
 
    }); 
 
    } else if (searchFldVal.length < 3) { 
 
    $('#searchMsg').addClass("error").show().text('You must enter at least 3 characters to search on.').delay(4000).fadeOut('slow').queue(function() { 
 
     $(this).removeClass("error").dequeue(); 
 
    }); 
 
    } else { 
 
    console.log('Search record.'); 
 
    } 
 
}
section.mainBox { 
 
    width: 100%; 
 
    padding-top: 0; 
 
    background-color: white; 
 
    border: 2px solid #000099; 
 
} 
 

 
div#Container { 
 
    padding-top: 8px; 
 
    padding-bottom: 8px; 
 
    text-align: center; 
 
    background-color: #B0C4DE; 
 
    border-bottom: 2px solid #000099; 
 
} 
 

 
div.nextTo { 
 
    display: inline-block; 
 
    margin-left: 5px; 
 
} 
 

 
div.nextTo:last-child { 
 
    position: absolute; 
 
    top: 6px; 
 
} 
 

 
span.msgMainBox { 
 
    display: none; 
 
    margin: 2px 5px; 
 
    padding: 2px 25px 2px 35px; 
 
} 
 

 
span.error { 
 
    border: 1px solid; 
 
    margin: 5px; 
 
    padding: 5px 10px 5px 40px; 
 
    background-repeat: no-repeat; 
 
    background-position: 10px center; 
 
    border-radius: 3px; 
 
    display: block; 
 
} 
 

 
span.error { 
 
    color: #D8000C; 
 
    background-color: #FFBABA; 
 
} 
 

 
@media all and (max-width:800px) { 
 
    div.nextTo:last-child { 
 
    position: static; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class="mainBox"> 
 
    <div id="Container"> 
 
    <div class="nextTo"> 
 
     <select name="menu" id="menu"> 
 
     <option value="1">Name</option> 
 
     <option value="2">DOB</option> 
 
     <option value="3">City</option> 
 
     </select> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="text" name="searchFld" id="searchFld" size="24" maxlength="24" value="" title="Maximum size of the field is 24 characters." placeholder="Example: Marco Polo"> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="button" name="searchBtn" id="searchBtn" value="Search"> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="button" name="lockBtn" id="lockBtn" value="Unlock" style="display:none"> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <span id="searchMsg" class="msgMainBox"></span> 
 
    </div> 
 
    </div> 
 
</section>

+0

ボタンで別のdivを使用しているとどうなりますか?私はそれを試して、そのdivを絶対に設定すると、そのdivにメッセージが表示されます。 –

+0

@エスプレッソコーヒーはあなたが手がけてくれますか?私はあなたを手に入れませんでした:) –

+0

それで、私にはロックされていないボタンがあります。一度それが画面に表示されると、すべてが拡大しても問題ありませんが、メッセージが表示されたあと、Unlockボタンはメッセージボックスと共に消えます。その場合、すべてがシフトします。だから私はUnlockボタンdivを絶対に設定しようとしましたが、その場合はメッセージがボタンの上に表示されます。 –

1

あなたはポジショニング、いくつかmargin秒に変更し、画面の幅を取得、padding@mediaクエリでそれを行うことができます狭すぎると、以下のエラーメッセージが表示され、水平方向に中央に表示されます。

$('#searchBtn').on('click',searchFun); 
 

 
function searchFun(){ 
 
    var menuVal = $.trim($('#menu').val()), 
 
\t searchFldVal = $.trim($('#searchFld').val()); 
 
    
 
    if(!searchFldVal){ 
 
\t \t $('#searchMsg').addClass("error").show().text('This field is required.').delay(4000).fadeOut('slow').queue(function(){ 
 
\t \t  $(this).removeClass("error").dequeue(); 
 
\t \t }); 
 
\t }else if(searchFldVal.length < 3){ 
 
\t \t $('#searchMsg').addClass("error").show().text('You must enter at least 3 characters to search on.').delay(4000).fadeOut('slow').queue(function(){ 
 
\t \t  $(this).removeClass("error").dequeue(); 
 
\t \t }); 
 
\t }else{ 
 
    console.log('Search record.'); 
 
    } 
 
}
section.mainBox{ 
 
\t width: 100%; 
 
\t padding-top: 0; 
 
\t background-color: white; 
 
\t border: 2px solid #000099; 
 
} 
 
div#Container { 
 
    position: relative; /* added */ 
 
\t padding-top: 8px; 
 
\t padding-bottom: 8px; 
 
\t text-align: center; 
 
\t background-color: #B0C4DE; 
 
\t border-bottom: 2px solid #000099; 
 
} 
 
div.nextTo { 
 
\t display: inline-block; 
 
\t margin-left: 5px; 
 
} 
 
span.msgMainBox { 
 
\t display: none; 
 
\t margin: 2px 5px; 
 
\t padding: 2px 25px 2px 35px; 
 
} 
 
span.error { 
 
\t border: 1px solid; 
 
    /* added */ 
 
    position: absolute; 
 
    top: 50%; 
 
    right: 0; 
 
    transform: translateY(-50%); /* vertically centered */ 
 
    margin: 0 5px; /* modified */ 
 
    padding: 0 5px; /* modified */ 
 
    /***/ 
 
\t background-repeat: no-repeat; 
 
\t background-position: 10px center; 
 
\t border-radius: 3px; 
 
\t display: block; 
 
} 
 
span.error { 
 
\t color: #D8000C; 
 
\t background-color: #FFBABA; 
 
} 
 
/* added */ 
 
@media screen and (max-width: 630px) { /* adjust to your needs */ 
 
    span.error { 
 
    top: calc(100% + 5px); /* + 5px "margin-top" */ 
 
    left: 50%; 
 
    transform: translateX(-50%); /* horizontally centered */ 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class="mainBox"> 
 
    <div id="Container"> 
 
    <div class="nextTo"> 
 
     <select name="menu" id="menu"> 
 
     <option value="1">Name</option> 
 
     <option value="2">DOB</option> 
 
     <option value="3">City</option> 
 
     </select> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="text" name="searchFld" id="searchFld" size="24" maxlength="24" value="" title="Maximum size of the field is 24 characters." placeholder="Example: Marco Polo" /> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="button" name="searchBtn" id="searchBtn" value="Search"/> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <input type="button" name="lockBtn" id="lockBtn" value="Unlock" style="display:none" /> 
 
    </div> 
 
    <div class="nextTo"> 
 
     <span id="searchMsg" class="msgMainBox"></span> 
 
    </div> 
 
    </div> 
 
</section>

関連する問題