2015-08-18 3 views
4

Есть мой код: https://jsfiddle.net/a4Le1jkz/.Держите плавающий элемент внизу

HTML

<div class="form-container"> 
<form action="#" method="#" id="form"> 
    <input type="image" src="http://image.noelshack.com/fichiers/2015/34/1439918200-send.png" alt="Send" id="send" class="send" /> 
    <div class="twrap"> 
     <textarea name="message" id="message" rows=1></textarea></div> 
    </form> 
</div> 

CSS

.form-container { 
    box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    position: absolute; 
    width: 100%; 
    color: white; 
    top: auto; 
    bottom: 0; 
    overflow: hidden; 
    padding: 10px; 
} 
.form-container .twrap { 
    overflow: hidden; 
    padding-right: 10px; 
} 

.form-container textarea { 
    width: 100%; 
    height: 30px; 
    line-height: 30px; 
    max-height: 120px; 
    font-size: 1.2em; 
    resize : none; 
} 

.form-container .send { 
    padding: 3px; 
    height: 30px; 
    float: right; 
} 

Я хотел бы сохранить изображение в нижней части textarea, даже если пользователь нажимает Enter. textearea должен продолжать регулировать свою ширину.

Изображение плавающее, поэтому я не знаю, как его сохранить внизу.

ответ

3

Вы должны поместить .send в position: absolute; bottom:10px; right:15px

и установить ширину текстового поля, чтобы Сюр они не перекрываются.

Здесь обновить ваш JsFiddle: https://jsfiddle.net/a4Le1jkz/1/

Edit: здесь обновление: https://jsfiddle.net/a4Le1jkz/7/ я установить width: 100% в текстовое поле и я добавить к .twrap padding-right равным места мне нужно для изображения справа ,

+1

При изменении размера окна, в текстовое поле и наложение изображений. – Neabfi

+0

Установка 'width' в textarea не очень полезна, так как она должна быть на 100% меньше ширины изображения. Вы можете исправить это с помощью CSS 'calc()', но многие браузеры этого не поддерживают. – Xufox

+0

Я отредактировал свой ответ, увидев ваш комментарий. – Beauceron

0

Лично я бы просто использовал таблицу вместо div. Ячейка таблицы может иметь значение CSS vertical-align:bottom.

<div class='form-container'> 
    <form action="#" method="#" id="form"> 
     <table style='width:100%'> 
      <tr> 
       <td class="twrap"><textarea name="message" id="message"></textarea></td> 
       <td style='vertical-align:bottom'><input type="image" src="http://image.noelshack.com/fichiers/2015/34/1439918200-send.png" alt="Send" id="send" class="send" /></td> 
      </tr> 
     </table> 
    </form> 
</div> 
+1

Хотя это может работать, имейте в виду, что таблицы предназначены для табличных данных, а не для макетов. – War10ck

+0

Вместо HTM следует использовать таблицу CSS. – Oriol

2

Вы установите на использовании float: right или вы бы быть открыты для display: inline-block;. С небольшой настройкой я смог заставить это работать, используя опцию display.

/*! 
 
\t Autosize 3.0.8 
 
\t license: MIT 
 
\t http://www.jacklmoore.com/autosize 
 
*/ 
 
!function(e,t){if("function"==typeof define&&define.amd)define(["exports","module"],t);else if("undefined"!=typeof exports&&"undefined"!=typeof module)t(exports,module);else{var o={exports:{}};t(o.exports,o),e.autosize=o.exports}}(this,function(e,t){"use strict";function o(e){function t(){var t=window.getComputedStyle(e,null);"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),u="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),i()}function o(t){var o=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=o,v=t,l&&(e.style.overflowY=t),n()}function n(){var t=window.pageYOffset,o=document.body.scrollTop,n=e.style.height;e.style.height="auto";var i=e.scrollHeight+u;return 0===e.scrollHeight?void(e.style.height=n):(e.style.height=i+"px",document.documentElement.scrollTop=t,void(document.body.scrollTop=o))}function i(){var t=e.style.height;n();var i=window.getComputedStyle(e,null);if(i.height!==e.style.height?"visible"!==v&&o("visible"):"hidden"!==v&&o("hidden"),t!==e.style.height){var r=document.createEvent("Event");r.initEvent("autosize:resized",!0,!1),e.dispatchEvent(r)}}var r=void 0===arguments[1]?{}:arguments[1],d=r.setOverflowX,s=void 0===d?!0:d,a=r.setOverflowY,l=void 0===a?!0:a;if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!e.hasAttribute("data-autosize-on")){var u=null,v="hidden",f=function(t){window.removeEventListener("resize",i),e.removeEventListener("input",i),e.removeEventListener("keyup",i),e.removeAttribute("data-autosize-on"),e.removeEventListener("autosize:destroy",f),Object.keys(t).forEach(function(o){e.style[o]=t[o]})}.bind(e,{height:e.style.height,resize:e.style.resize,overflowY:e.style.overflowY,overflowX:e.style.overflowX,wordWrap:e.style.wordWrap});e.addEventListener("autosize:destroy",f),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",i),window.addEventListener("resize",i),e.addEventListener("input",i),e.addEventListener("autosize:update",i),e.setAttribute("data-autosize-on",!0),l&&(e.style.overflowY="hidden"),s&&(e.style.overflowX="hidden",e.style.wordWrap="break-word"),t()}}function n(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=document.createEvent("Event");t.initEvent("autosize:destroy",!0,!1),e.dispatchEvent(t)}}function i(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=document.createEvent("Event");t.initEvent("autosize:update",!0,!1),e.dispatchEvent(t)}}var r=null;"undefined"==typeof window||"function"!=typeof window.getComputedStyle?(r=function(e){return e},r.destroy=function(e){return e},r.update=function(e){return e}):(r=function(e,t){return e&&Array.prototype.forEach.call(e.length?e:[e],function(e){return o(e,t)}),e},r.destroy=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],n),e},r.update=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],i),e}),t.exports=r}); 
 
autosize($('#message'));
.form-container { 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
    position: absolute; 
 
    width: 100%; 
 
    color: white; 
 
    top: auto; 
 
    bottom: 0; 
 
    overflow: hidden; 
 
    padding: 10px; 
 
} 
 
\t \t 
 
.form-container .twrap { 
 
    overflow: hidden; 
 
    padding-right: 10px; 
 
    display: inline-block; 
 
    width: 90%; 
 
} 
 

 
.form-container textarea { 
 
    width: 100%; 
 
    height: 30px; 
 
    line-height: 30px; 
 
    max-height: 120px; 
 
    font-size: 1.2em; 
 
    vertical-align: middle; 
 
    resize : none; 
 
} 
 

 
.form-container .send { 
 
    padding: 3px; 
 
    height: 30px; 
 
    display: inline-block; 
 
    vertical-align: bottom; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-container"> 
 
    <form action="#" method="#" id="form"> 
 
     <div class="twrap"> 
 
      <textarea name="message" id="message" rows=1></textarea> 
 
     </div><input type="image" src="http://image.noelshack.com/fichiers/2015/34/1439918200-send.png" alt="Send" id="send" class="send" /> 
 
    </form> 
 
</div>

были сделаны следующие изменения:

HTML:

<div class="twrap"> был перенесен перед входом.

CSS:

display: inline-block; и width: 90% были добавлены в .form-container .twrap CSS.

display: inline-block; и vertical-align: bottom; были добавлены в .form-container .send CSS. Кроме того, из этого CSS был удален float: right;.

+0

это хороший, но он не будет работать очень хорошо при разных разрешениях. Я думаю, что абсолютное решение позиции обеспечивает максимальную гибкость в этом сценарии. –

2

Использование Flexbox вы легко добиться того, что вы хотите, как это:

/*! 
 
\t Autosize 3.0.8 
 
\t license: MIT 
 
\t http://www.jacklmoore.com/autosize 
 
*/ 
 
! function (e, t) { 
 
    if ("function" == typeof define && define.amd) define(["exports", "module"], t); 
 
    else if ("undefined" != typeof exports && "undefined" != typeof module) t(exports, module); 
 
    else { 
 
     var o = { 
 
      exports: {} 
 
     }; 
 
     t(o.exports, o), e.autosize = o.exports 
 
    } 
 
}(this, function (e, t) { 
 
    "use strict"; 
 

 
    function o(e) { 
 
     function t() { 
 
      var t = window.getComputedStyle(e, null); 
 
      "vertical" === t.resize ? e.style.resize = "none" : "both" === t.resize && (e.style.resize = "horizontal"), u = "content-box" === t.boxSizing ? -(parseFloat(t.paddingTop) + parseFloat(t.paddingBottom)) : parseFloat(t.borderTopWidth) + parseFloat(t.borderBottomWidth), i() 
 
     } 
 
     function o(t) { 
 
      var o = e.style.width; 
 
      e.style.width = "0px", e.offsetWidth, e.style.width = o, v = t, l && (e.style.overflowY = t), n() 
 
     } 
 
     function n() { 
 
      var t = window.pageYOffset, 
 
       o = document.body.scrollTop, 
 
       n = e.style.height; 
 
      e.style.height = "auto"; 
 
      var i = e.scrollHeight + u; 
 
      return 0 === e.scrollHeight ? void(e.style.height = n) : (e.style.height = i + "px", document.documentElement.scrollTop = t, void(document.body.scrollTop = o)) 
 
     } 
 
     function i() { 
 
      var t = e.style.height; 
 
      n(); 
 
      var i = window.getComputedStyle(e, null); 
 
      if (i.height !== e.style.height ? "visible" !== v && o("visible") : "hidden" !== v && o("hidden"), t !== e.style.height) { 
 
       var r = document.createEvent("Event"); 
 
       r.initEvent("autosize:resized", !0, !1), e.dispatchEvent(r) 
 
      } 
 
     } 
 
     var r = void 0 === arguments[1] ? {} : arguments[1], 
 
      d = r.setOverflowX, 
 
      s = void 0 === d ? !0 : d, 
 
      a = r.setOverflowY, 
 
      l = void 0 === a ? !0 : a; 
 
     if (e && e.nodeName && "TEXTAREA" === e.nodeName && !e.hasAttribute("data-autosize-on")) { 
 
      var u = null, 
 
       v = "hidden", 
 
       f = function (t) { 
 
        window.removeEventListener("resize", i), e.removeEventListener("input", i), e.removeEventListener("keyup", i), e.removeAttribute("data-autosize-on"), e.removeEventListener("autosize:destroy", f), Object.keys(t).forEach(function (o) { 
 
         e.style[o] = t[o] 
 
        }) 
 
       }.bind(e, { 
 
        height: e.style.height, 
 
        resize: e.style.resize, 
 
        overflowY: e.style.overflowY, 
 
        overflowX: e.style.overflowX, 
 
        wordWrap: e.style.wordWrap 
 
       }); 
 
      e.addEventListener("autosize:destroy", f), "onpropertychange" in e && "oninput" in e && e.addEventListener("keyup", i), window.addEventListener("resize", i), e.addEventListener("input", i), e.addEventListener("autosize:update", i), e.setAttribute("data-autosize-on", !0), l && (e.style.overflowY = "hidden"), s && (e.style.overflowX = "hidden", e.style.wordWrap = "break-word"), t() 
 
     } 
 
    } 
 
    function n(e) { 
 
     if (e && e.nodeName && "TEXTAREA" === e.nodeName) { 
 
      var t = document.createEvent("Event"); 
 
      t.initEvent("autosize:destroy", !0, !1), e.dispatchEvent(t) 
 
     } 
 
    } 
 
    function i(e) { 
 
     if (e && e.nodeName && "TEXTAREA" === e.nodeName) { 
 
      var t = document.createEvent("Event"); 
 
      t.initEvent("autosize:update", !0, !1), e.dispatchEvent(t) 
 
     } 
 
    } 
 
    var r = null; 
 
    "undefined" == typeof window || "function" != typeof window.getComputedStyle ? (r = function (e) { 
 
     return e 
 
    }, r.destroy = function (e) { 
 
     return e 
 
    }, r.update = function (e) { 
 
     return e 
 
    }) : (r = function (e, t) { 
 
     return e && Array.prototype.forEach.call(e.length ? e : [e], function (e) { 
 
      return o(e, t) 
 
     }), e 
 
    }, r.destroy = function (e) { 
 
     return e && Array.prototype.forEach.call(e.length ? e : [e], n), e 
 
    }, r.update = function (e) { 
 
     return e && Array.prototype.forEach.call(e.length ? e : [e], i), e 
 
    }), t.exports = r 
 
}); 
 
autosize($('#message'));
.form-container { 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
    position: absolute; 
 
    width: 100%; 
 
    color: white; 
 
    top: auto; 
 
    bottom: 0; 
 
    overflow: hidden; 
 
    padding: 10px; 
 
} 
 
#form { 
 
    display: flex; 
 
    align-items: flex-end; 
 
} 
 
.form-container .twrap { 
 
    overflow: hidden; 
 
    padding-right: 10px; 
 
    flex-grow: 1; 
 
} 
 
.form-container textarea { 
 
    width: 100%; 
 
    height: 30px; 
 
    line-height: 30px; 
 
    max-height: 120px; 
 
    font-size: 1.2em; 
 
    vertical-align: middle; 
 
    resize : none; 
 
} 
 
.form-container .send { 
 
    padding: 3px; 
 
    height: 30px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-container"> 
 
    <form action="#" method="#" id="form"> 
 
     <div class="twrap"> 
 
      <textarea name="message" id="message" rows=1></textarea> 
 
     </div> 
 
     <input type="image" src="http://image.noelshack.com/fichiers/2015/34/1439918200-send.png" alt="Send" id="send" class="send" /> 
 
    </form> 
 
</div>

Here a working jsfiddle to play with.

+0

Почему это было downvoted? ... – War10ck

+0

Я не тот, кто занижен - кажется надежным решением для меня. Я предполагаю, что причина для downvote заключается в отсутствии широкой поддержки браузером технологии flexbox. – Xufox

+0

@Xufox Я думаю, что это не повод, чтобы проголосовать за ответ и проверить это для поддержки браузера https://css-tricks.com/snippets/css/a-guide-to-flexbox/#browser-support, все современные браузеры поддерживают его. –

Смежные вопросы