js动态创建css样式

function setCssText(css){ 
    if(document.all){ // document.createStyleSheet(url)
        window.style = css
        document.createStyleSheet("javascript:style")
    }else{ //document.createElement(style)
        var style = document.createElement('style')
        style.type = 'text/css'
        style.innerHTML = css
        document.getElementsByTagName('HEAD').item(0).appendChild(style)
    } 
}
setCssText('body { color: red }')

position:sticky实现特殊的业务需求

这是一个结合了 position:relative 和 position:fixed 两种定位功能于一体的特殊定位,适用于一些特殊场景。

当父元素部分滚动到”视线范围外”时,开始实现「fixed」的功能。当父元素整个不在“实现范围内”,取消「fixed」的功能。


<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>sticky</title> <style> .parent { display: flex; /*注释这个瞧瞧瞧瞧*/ } .left { background: red; height: 500px; width: 220px; position: sticky; top: 20px; /*须指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效*/ z-index: -1; } .right { background: blue; height: 1200px; flex: 1; } </style> </head> <body> <div style="height: 200px;"></div> <div class="parent"> <div class="left"> </div> <div class="right"> </div> </div> <div style="height: 1200px;"></div> </body> </html>

利用background-attachment:fixed做页面效果

background-attachment:

  • scroll 默认值。背景图像会随着页面其余部分的滚动而移动。
  • fixed 当页面的其余部分滚动时,背景图像不会移动。
    <section class="banner">

    </section>

    <div class="content">

    </div>
    .banner {
        background-image: url(banner.jpg);
        background-size: cover;
        background-attachment: fixed;
        height: 400px;
    }

    .content {
        height: 1000px;
        background-color: red;
    }

java继承对属性的访问

Parent.java

@Data
public class Parent {

    private int size = -1;

    public void printSize() {
        System.out.println(getSize());
    }

    public void printSize2() {
        System.out.println(size);
    }
}

Son.java

@Data
public class Son extends Parent {

    private int size = 15;

    public static void main(String[] args) {
        Son s = new Son();

        s.printSize(); // 15
        s.printSize2(); // -1 访问父类的size属性
    }
}

display:table && display:table-cell 实现布局

我们都知道css3支持flex布局,可以实现水平和垂直居中的场景。但是对于老的浏览器可能不支持flex属性,我们可以通过display: tabledisplay: table-cell来布局。

代码片段如下:

.container {
   /* display: flex;
    align-items: center;*/
    display: table;
    width: 100%;
    height: 200px;
    background-color: red;
}

.block {
    display: table-cell;
    vertical-align: middle;
    width: 50%;
}
<div class="container">
    <div class="block">
        <h1>hello</h1>
        <span>world</span>
    </div>
    <div class="block">
        <h1>Shit</h1>
        <span>HaHa</span>
    </div>
</div>