2015年1月

根据文本域(Textarea)内容自动调整高度

  • html部分
<textarea data-adaptheight rows="3" cols="40" placeholder="Your input"
style="padding: 16px; line-height: 1.5;"></textarea>
  • js部分
(function() {
    function adjustHeight(textareaElement, minHeight) {
        // compute the height difference which is caused by border and outline
        var outerHeight = parseInt(window.getComputedStyle(el).height, 10);
        var diff = outerHeight - el.clientHeight;

        // set the height to 0 in case of it has to be shrinked
        el.style.height = 0;

        // set the correct height
        // el.scrollHeight is the full height of the content, not just the visible part
        el.style.height = Math.max(minHeight, el.scrollHeight + diff) + 'px';
    }


    // we use the "data-adaptheight" attribute as a marker
    var textAreas = document.querySelectorAll('textarea[data-adaptheight]');

    // iterate through all the textareas on the page
    for (var i = 0, l = textAreas.length; i < l; i++) {
        var el = textAreas[i];

        // we need box-sizing: border-box, if the textarea has padding
        el.style.boxSizing = el.style.mozBoxSizing = 'border-box';

        // we don't need any scrollbars, do we? :)
        el.style.overflowY = 'hidden';

        // the minimum height initiated through the "rows" attribute
        var minHeight = el.scrollHeight;

        el.addEventListener('input', function() {
            adjustHeight(el, minHeight);
        });

        // we have to readjust when window size changes (e.g. orientation change)
        window.addEventListener('resize', function() {
            adjustHeight(el, minHeight);
        });

        // we adjust height to the initial content
        adjustHeight(el, minHeight);
    }
}());

示例:http://jsfiddle.net/5h4fauq8/1/
原文:http://web.jobbole.com/81906/

jQuery动态固定页面元素

原文出自,可参考其具体实例 http://www.cnblogs.com/Hodor/archive/2012/07/25/2607831.html


// 固定顶部
$('#order-lefttitle').parent().parent().parent().scrollFix('top','top');
// 固定底部
$('#order-footerinfo').scrollFix('bottom','bottom');

;(function($) {
  jQuery.fn.scrollFix = function(height, dir) {
    height = height || 0;
    height = height == "top" ? 0 : height;
    return this.each(function() {
      if (height == "bottom") {
        height = document.documentElement.clientHeight - this.scrollHeight;
      } else if (height < 0) {
        height = document.documentElement.clientHeight - this.scrollHeight + height;
      }
      var that = $(this),
        oldHeight = false,
        p, r, l = that.offset().left;
      dir = dir == "bottom" ? dir : "top"; //默认滚动方向向下
      if (window.XMLHttpRequest) { //非ie6用fixed


        function getHeight() { //>=0表示上面的滚动高度大于等于目标高度
          return (document.documentElement.scrollTop || document.body.scrollTop) + height - that.offset().top;
        }
        $(window).scroll(function() {
          if (oldHeight === false) {
            if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) {
              oldHeight = that.offset().top - height;
              that.css({
                position: "fixed",
                top: height,
                left: l
              });
            }
          } else {
            if (dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) {
              that.css({
                position: "static"
              });
              oldHeight = false;
            } else if (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight) {
              that.css({
                position: "static"
              });
              oldHeight = false;
            }
          }
        });
      } else { //for ie6
        $(window).scroll(function() {
          if (oldHeight === false) { //恢复前只执行一次,减少reflow
            if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) {
              oldHeight = that.offset().top - height;
              r = document.createElement("span");
              p = that[0].parentNode;
              p.replaceChild(r, that[0]);
              document.body.appendChild(that[0]);
              that[0].style.position = "absolute";
            }
          } else if ((dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) || (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight)) { //结束
            that[0].style.position = "static";
            p.replaceChild(that[0], r);
            r = null;
            oldHeight = false;
          } else { //滚动
            that.css({
              left: l,
              top: height + document.documentElement.scrollTop
            })
          }
        });
      }
    });
  };
})(jQuery);

js判断页面是否有滚动条

前端经常各种调试,有时候直接判断页面是否有滚动条能直接解决问题
4 是滚动条边框所占像素

// 是否有竖向滚动条
if (document.documentElement.clientHeight < document.documentElement.offsetHeight-4){

    //执行相关脚本。

}

// 是否有横向滚动条
if (document.documentElement.clientWidth < document.documentElement.offsetWidth-4){

    //执行相关脚本。

}

PHP的mail函数

// 发送邮件
function send($mails){
    $to = 'a@a.com, b@b.com';
    $title = '检查结果';
    $content = "以下订单号有错误" . "\r\n";
    $content .= "id:1,2,3";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
    $headers .= "From: system@system.com";    // 自定义
    $r = mail($to, $title, $content, $headers);

    var_dump($r);
}

注意的问题


  • 标题做编码处理(不同客户端的编码不同,可能会乱码)
$subject = iconv('','GB2312','亲爱的'.$s_user.',请取回您的密码!');
  • Linux环境
    ·linux·下注意·/usr/sbin/sendmail· 或者·/usr/bin/sendmail·是否存在

若不存在可能是没安装

sudo apt-get install sendmail

然后就可以直接用 PHP 内置的mail()函数了

  • php.ini 配置
[mail function]
; For Win32 only.
SMTP = smtp.126.com
#smtp_port = 25

; For Win32 only.
sendmail_from = jiangwei8909@126.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i ").
;sendmail_path =

前三项对windows环境生效 sendmail_pathunix生效

PHP执行时间类

其实也就是通过microtime函数在前后个执行一下,计算时间差获取代码段的执行时间,测试效率

$runtime->stop();
echo $_b;
echo "页面执行时间: ".$runtime->spent()." 毫秒";

class runtime
{
    var $StartTime = 0;
    var $StopTime = 0;
    function get_microtime()
    {
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
    }

    function start()
    {
        $this->StartTime = $this->get_microtime();
    }

    function stop()
    {
        $this->StopTime = $this->get_microtime();
    }

    function spent()
    {
        return round(($this->StopTime - $this->StartTime) * 1000, 1);
    }
}

原文出自开源中国