js

243 阅读3分钟

1.现有一个数组,包含多个对象,每个对象都有score,age两项,对应值都是数字类型,对该数组使用sort方法进行排序,要求score大大排前面,score相同,age小的排前面,写一下实现

2.给当前js运行环境的所有字符串拓展count方法,接受一个字符串,返回字符串在原字符串中出现的次数

3.统计‘abcdfadfddddfadfddddd’一个字符串中出现字母最多的一个字母,并把它出现的个数和空上字母打印出来

4.手写html和css 使一个宽高为300的div在浏览器中水平垂直居中

  • 方法1: 伪元素+inline-block+vertically --- 用一个“ghost”伪元素(看不见的伪元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙。但是这个方法要求待居中的元素是 inline-block,不是一个真正通用的方案。
  <style>
    .content {
      width: 500px;
      height: 500px;
      background-color: #000;
      text-align: center;
    }
    .box {
      background-color: #daa520;
      display: inline-block;
      vertical-align: middle;
      width: 70%;
    }
    .content:before {
      content: "";
      display: inline-block;
      vertical-align: middle;
      height: 100%;
    }
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="content">用一个“ghost”伪元素(看不见的伪元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙。但是这个方法要求待居中的元素是 inline-block,不是一个真正通用的方案。</div>
  </div>
</body>
  • 方法2:css3--flex
  <style>
    .wrapper {
      width: 500px;
      height: 500px;
      background-color: #000;
      justify-content: center;
      align-items: center;
      display: -webkit-flex;
    }
    .content {
      background-color: #daa520;
      width: 50%;
    }
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="content">css3--flex
    </div>
  </div>
</body>
  • **方法3: 子绝父相定位 + margin: auto **

(margin的值设置为auto,可以让我们对剩余空间进行分配!)

  <style>
    .wrapper {
      width: 500px;
      height: 500px;
      background-color: #000;
      position: relative;
    }
    .content {
      background-color: #daa520;
      width: 70%;
      margin: auto;
      height: 50%;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }
  </style>
</head>
<body>
  <div class="wrapper">
      <div class="content">
        margin: auto + 定位 <br>
        margin的值设置为auto,可以让我们对剩余空间进行分配!我们知道,块级元素设置为margin:0
        auto;可以左右居中显示!那有没有办法让margin设置为margin:auto之后,上下左右都居中呢?上下左右都居中,就可以实现我们的垂直居中了!
        答案是有的,只要我们让上下有足够的空间,就可以让margin的auto来分配上下空间。
        我们可以利用定位的方式,让margin上下左右都有足够的空间!那么就可以用margin:auto来实现垂直居中了!
      </div>
  </div>
</body>
    1. 子绝父相定位 + 子50% + transform: translate(-50%,-50%)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .wrapper {
      width: 500px;
      height: 500px;
      background-color: #000;
      position: relative;
    }
    .box {
      background-color: #daa520;
      position: absolute;
      width: 70%;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      -webkit-transform: translateX(-50%) translateY(-50%);
      -moz-transform: translateX(-50%) translateY(-50%);
      -ms-transform: translateX(-50%) translateY(-50%);
      transform: translateX(-50%) translateY(-50%);
    }
  </style>
</head>
<body>
  <div class="wrapper">
    <div class="box">方法是用的css3的写法,想兼容IE8的还是建议用上面的方法!<br>方法和我们固定高宽的差不多,但是不用margin我们用的是 translate()</div>
  </div>
</body>
</html>

html