Home > Web Front-end > Layui Tutorial > Introduction to fine-tuning source code compatibility of layui framework

Introduction to fine-tuning source code compatibility of layui framework

Release: 2019-11-23 12:02:11
forward
2466 people have browsed it

layui provides a wealth of built-in modules, all of which can be loaded on demand through a modular approach. This tutorial provides you with some introduction to fine-tuning the source code compatibility of the layui framework. I hope it can solve the problems you encounter when using the layui framework. compatibility issues.

Introduction to fine-tuning source code compatibility of layui framework

table.js

1. Description: [All browsers] The loading icon is static and I want to make it animated.

Solution: Find each "layui-icon-loading" and add "layui-anim layui-anim-rotate layui-anim-loop"

2. Description: [ Firefox and IE10] If the cell is plain text, then the number of omitted words at the end needs to be more than 3 characters before the layer.tips pop-up window will be triggered.

Solution: Because "scrollWidth" is used internally, its performance in these two programs is different from that of Google.

Now calculate the pixels by calculating the internal text length

// 源码 table.js 1600 左右开始
var othis = $(this)
,elemCell = othis.children(ELEM_CELL);
// m
var computedFontWidth = function() {
  var fontSize = parseFloat(elemCell.css('font-size'))
  var text = elemCell.text()
  var width = 0, i = 0, len = text.length
  while ( i < len) {
    // 基础像素,我的是 14px,不知道可以看 html 或者 body 内的字体大小设置。
    // 火狐和IE中字体的长度就是双字节文字按基础像素计算,单字节文字接触像素/2。
    // 谷歌比较另类,双字节文字按基础像素计算,单字节是基础像素/2与 16/2 的差值,
    // 不知道是不是我想复杂了,所以用 +1,微调下浏览器的差异
    width += text.charAt(i).match(/[^\x00-\xff]/ig) != null ? fontSize : (fontSize / 2 + 1)
    i++
   }
  return width
}

var computedScrollWidth = function() {
  var hasChildren = elemCell.children().length > 0
  var originScrollWidth = elemCell.prop(&#39;scrollWidth&#39;)
  var realScrollWidth = hasChildren 
    ? 0
    : (computedFontWidth() + parseFloat(elemCell.css(&#39;padding-left&#39;)) + parseFloat(elemCell.css(&#39;padding-right&#39;)))
  // 这里比较大小值,为了对于原来非纯文本还是采用 layui 原本的方式
  return Math.max(originScrollWidth, realScrollWidth)
}

if(hide){
  othis.find(&#39;.layui-table-grid-down&#39;).remove();
// } else if(elemCell.prop(&#39;scrollWidth&#39;) > elemCell.outerWidth()){ // 注释掉原本的代码
} else if(computedScrollWidth() > elemCell.outerWidth()){
  if(elemCell.find(&#39;.&#39;+ ELEM_GRID_DOWN)[0]) return;
  othis.append(&#39;<div class="&#39;+ ELEM_GRID_DOWN +&#39;"><i class="layui-icon layui-icon-down"></i></div>&#39;);
}
Copy after login

form.js

1. Description: [All browsers] Positioning of select, if select is in the project On the far right, in the lower right corner, clicking will cause a scroll bar to appear on the non-scrolling page

Solution:

// 修改部分定位写法
var showDown = function(){
  // 将样式复位
  dl.css({
    top: &#39;&#39;,
    left: &#39;&#39;,
    right: &#39;&#39;
  })
  var top = reElem.offset().top + reElem.outerHeight() + 5 - $win.scrollTop()
  ,dlHeight = dl.outerHeight(), dlWidth = dl.outerWidth(), winWidth = $(window).width();
 
  index = select[0].selectedIndex; //获取最新的 selectedIndex

   // 判断是否在最右边的临界点
  if (dlWidth + reElem.offset().left > winWidth) {
    dl.css({
      left: &#39;auto&#39;,
      right: 0
    })
  }
  reElem.addClass(CLASS+&#39;ed&#39;);
  dds.removeClass(HIDE);
  nearElem = null;
  
  //初始选中样式
  dds.eq(index).addClass(THIS).siblings().removeClass(THIS);

  //上下定位识别
  if(top + dlHeight > $win.height() && top >= dlHeight){
    reElem.addClass(CLASS + &#39;up&#39;);
  } else {
    // 定位重写定义
  
    // 动画需要的距离和弹窗的间隔,这是根据 layui 的样式来的,理论应该计算
    var ANIM_UP = 30, SPACE = 5; 
    var reElemBottom = reElem.get(0).getBoundingClientRect().bottom;
    var dlTop = parseInt(dl.css(&#39;top&#39;));
    var winHeight = $win.height();
    if (reElemBottom + dlHeight + ANIM_UP > winHeight ) {
      var computeValue = dlTop - (reElemBottom + SPACE - (winHeight - dlHeight - ANIM_UP))
      dl.css(&#39;top&#39;, computeValue)
    }
  }
  followScroll();
}
Copy after login

tree.js

Description: I am using the layui2.4.5 version , the internal tree function has been satisfied, but because of the icon problem, I simply hide it here

Solution:

// 第一处修改
Tree.prototype.tree = function() {
   // 大概在 line 48
   var li = $([&#39;<li &#39;+ (hasChild ? " is-branch " : " is-leaf ") + (item.spread ? &#39;data-spread="&#39;+ item.spread +&#39;"&#39; : &#39;&#39;) +&#39;>&#39;
}

// 第二处修改

// 大概在 72 - 76 注释掉

/*+ (&#39;<i class="layui-icon layui-tree-&#39;+ (hasChild ? "branch" : "leaf") +&#39;">&#39;+ (
         hasChild ? ( item.spread ? icon.branch[1] : icon.branch[0]) : icon.leaf) +&#39;</i>&#39;) */ //节点图标
Copy after login

For more layui framework knowledge, please pay attention to layui framework tutorial.

The above is the detailed content of Introduction to fine-tuning source code compatibility of layui framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:layui.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template