Home > Article > Web Front-end > Introduction to fine-tuning source code compatibility of layui framework
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.
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('scrollWidth') var realScrollWidth = hasChildren ? 0 : (computedFontWidth() + parseFloat(elemCell.css('padding-left')) + parseFloat(elemCell.css('padding-right'))) // 这里比较大小值,为了对于原来非纯文本还是采用 layui 原本的方式 return Math.max(originScrollWidth, realScrollWidth) } if(hide){ othis.find('.layui-table-grid-down').remove(); // } else if(elemCell.prop('scrollWidth') > elemCell.outerWidth()){ // 注释掉原本的代码 } else if(computedScrollWidth() > elemCell.outerWidth()){ if(elemCell.find('.'+ ELEM_GRID_DOWN)[0]) return; othis.append('<div class="'+ ELEM_GRID_DOWN +'"><i class="layui-icon layui-icon-down"></i></div>'); }
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: '', left: '', right: '' }) 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: 'auto', right: 0 }) } reElem.addClass(CLASS+'ed'); dds.removeClass(HIDE); nearElem = null; //初始选中样式 dds.eq(index).addClass(THIS).siblings().removeClass(THIS); //上下定位识别 if(top + dlHeight > $win.height() && top >= dlHeight){ reElem.addClass(CLASS + 'up'); } else { // 定位重写定义 // 动画需要的距离和弹窗的间隔,这是根据 layui 的样式来的,理论应该计算 var ANIM_UP = 30, SPACE = 5; var reElemBottom = reElem.get(0).getBoundingClientRect().bottom; var dlTop = parseInt(dl.css('top')); var winHeight = $win.height(); if (reElemBottom + dlHeight + ANIM_UP > winHeight ) { var computeValue = dlTop - (reElemBottom + SPACE - (winHeight - dlHeight - ANIM_UP)) dl.css('top', computeValue) } } followScroll(); }
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 = $(['<li '+ (hasChild ? " is-branch " : " is-leaf ") + (item.spread ? 'data-spread="'+ item.spread +'"' : '') +'>' } // 第二处修改 // 大概在 72 - 76 注释掉 /*+ ('<i class="layui-icon layui-tree-'+ (hasChild ? "branch" : "leaf") +'">'+ ( hasChild ? ( item.spread ? icon.branch[1] : icon.branch[0]) : icon.leaf) +'</i>') */ //节点图标
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!