Web前端项目开发实战笔记(持续更新中,建议收藏)
(1)hover盒子,放大+阴影效果
transform : scale ( 1.05) ;
box-shadow : 0px 0px 10px 5px #dddddd;
(2)修改select中文本内容,option文本内容距离左边框间距
padding-left : 20px
(3)ajax请求访问服务器图片被防爬虫设置干扰,可以拿到图片url但是无法让图片显示
< meta name = " referrer" content = " never" >
(4)css绘制三角形
width : 0px;
border-bottom : 10px solid white;
border-top : 10px solid transparent;
border-left : 10px solid transparent;
border-right : 10px solid transparent;
(5)让盒子固定在屏幕中心
position : fixed;
margin : auto;
top : 0;
left : 0;
bottom : 0;
right : 0;
(6)css绘制1/4圆
width : 100px;
height : 100px;
border-radius : 0px 0px 0px 100px;
(7)css绘制半圆
width : 100px;
height : 200px;
border-radius : 100px 0px 0px 100px;
(8)H5画布canvas绘制随机验证码,点击切换验证码
< canvas width = " 80px" height = " 46px" id = " login_vCode" > </ canvas>
var loginSelector = document. querySelector ( "#login_vCode" ) ;
drawCode ( loginSelector, randomCode ( ) ) ;
$ ( loginSelector) . on ( "click" , function ( ) {
clearCode ( loginSelector) ;
drawCode ( loginSelector, randomCode ( ) ) ;
} )
function randomCode ( ) {
var charArr = [ "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "q" , "w" , "e" , "r" , "t" , "y" , "u" , "i" , "o" , "p" , "a" , "s" , "d" , "f" , 'g' , "h" , "j" , "k" , "l" , "z" , "x" , "c" ,
"v" , "b" , "n" , "m" , "Q" , "W" , "E" , "R" , "T" , "Y" , "U" , "I" , "O" , "P" , "A" , "S" , "D" , "F" , "G" , "H" , "J" , "K" , "L" , "Z" , "X" , "C" , "V" , "B" , "N" , "M" ] ;
var code= '' ;
for ( var count = 1 ; count < 5 ; count++ ) {
code+= charArr[ parseInt ( Math. random ( ) * charArr. length) ] ;
}
return code;
}
function drawCode ( selector, code) {
var ctx = selector. getContext ( "2d" ) ;
for ( var index= 0 ; index< code. length; index++ ) {
var fontSize = parseInt ( Math. random ( ) * 2 + 20 ) ;
var x = parseInt ( Math. random ( ) * ( 20 - fontSize) + ( index* 20 ) ) ;
var y = parseInt ( Math. random ( ) * ( 46 - fontSize) + fontSize) ;
ctx. fillStyle = `rgb( ${
parseInt ( Math. random ( ) * 256 ) } , ${
parseInt ( Math. random ( ) * 256 ) } , ${
parseInt ( Math. random ( ) * 256 ) } )` ;
ctx. font = fontSize+ 'px 楷体' ;
ctx. fillText ( code. charAt ( index) , x, y) ;
}
}
function clearCode ( selector) {
var ctx = selector. getContext ( '2d' ) ;
ctx. clearRect ( 0 , 0 , selector. width, selector. height) ;
}
(9)css禁用样式
cursor : not-allowed;
(10)弹性盒子,控制子盒子固定6个换行,两端对齐
display : flex;
flex-wrap : wrap;
justify-content : space-between;
width : 15%;
(11)三个radio单选按钮切换
< input type = " radio" id = " hot" name = " sort" > < label for = " hot" id = " hot-label" > 按评分降序</ label>
< input type = " radio" id = " time" name = " sort" > < label for = " time" id = " time-label" > 按时长升序</ label>
< input type = " radio" id = " price" name = " sort" > < label for = " price" id = " price-label" > 按票价升序</ label>
(12)js去掉末尾特定字符
str = str. substring ( 0 , str. lastIndexOf ( '特定字符' ) ) ;
(13) js数组去重
function fn ( arr) {
var newArr= [ ] ;
for ( let i = 0 ; i < arr. length; i++ ) {
if ( newArr. indexOf ( arr[ i] ) === - 1 ) {
newArr. push ( arr[ i] )
}
}
return newArr
}
new Set ( arr)
(14)标题左立样式
h5 {
font-size : 18px;
font-weight : bold;
color : #333333;
margin-bottom : 28px;
&::before {
display : block;
content : '' ;
float : left;
width : 4px;
height : 18px;
background-color : #f99135;
margin-right : 5px;
margin-top : 2px;
}
}
(15)span左图标样式
span {
display : inline-block;
width : 40px;
height : 13px;
text-align : right;
line-height : 13px;
color : #666;
font-size : 13px;
background-image : url(../img/cinema/btn-1.png) ;
background-repeat : no-repeat;
background-position : center left;
}
(16)绘制百度地图
< script src = " https://api.map.baidu.com/api?type=webgl&v=1.0&ak=gAidgw1xHZv7xXd4LMB2ithW6zFqp24n" > </ script>
< div id = " cine-map" > </ div>
width : 200px;
height : 200px;
var cineMap = new BMapGL. Map ( "cine-map" ) ;
cineMap. centerAndZoom ( "双流区星空路二段万达广场四楼" , 15 ) ;
cineMap. enableScrollWheelZoom ( true ) ;
var geo = new BMapGL. Geocoder ( )
geo. getPoint ( "双流区星空路二段万达广场四楼" , function ( point) {
var marker = new BMapGL. Marker ( new BMapGL. Point ( point. lng, point. lat) )
cineMap. addOverlay ( marker)
} , "成都市" )
(17)vscode中修改live serves插件浏览器
①打开vscode的setting
②搜索browser
③选择Live Server › Settings: Custom Browser修改即可
(18)解析浏览器URL中search数据
function getParam ( key) {
var search = decodeURIComponent ( location. search) ;
search= search. substring ( 1 ) ;
var params= search. split ( '&' ) ;
var result= null ;
params. forEach ( function ( val) {
var param= val. split ( '=' ) ;
if ( key== param[ 0 ] ) {
result= param[ 1 ] ;
}
} ) ;
return result;
}
(19)js获取字符串子串的个数
function fn ( str, s) {
let res = str. match ( /s/g ) ;
let count = ! res ? 0 : res. length;
return count;
}
(20)引入Google 字体库(免费的 Web 字体库-科学上网)
< link href = " https://fonts.googleapis.com/css?family=Lobster" rel = " stylesheet" type = " text/css" >
(21)Data()自定义时间格式
function getTime ( str, date) {
var now = date;
str = str. replaceAll ( /YYYY/g , now. getFullYear ( ) ) ;
str = str. replaceAll ( /MM/g , now. getMonth ( ) + 1 ) ;
str = str. replaceAll ( /DD/g , now. getDate ( ) ) ;
str = str. replaceAll ( /HH/g , now. getHours ( ) ) ;
str = str. replaceAll ( /mm/g , now. getMinutes ( ) ) ;
str = str. replaceAll ( /SS/g , now. getSeconds ( ) ) ;
return str;
}
console. log ( getTime ( "YYYY/MM/DD HH:MM:SS" , new Date ( ) ) ) ;
(22)css绘制❤型
< div class = " heart" > </ div>
.heart {
position : absolute;
margin : auto;
top : 0;
right : 0;
bottom : 0;
left : 0;
background-color : pink;
height : 50px;
width : 50px;
transform : rotate ( -45deg) ;
}
.heart::after {
background-color : pink;
content : "" ;
border-radius : 50%;
position : absolute;
width : 50px;
height : 50px;
top : 0px;
left : 25px;
}
.heart::before {
content : "" ;
background-color : pink;
border-radius : 50%;
position : absolute;
width : 50px;
height : 50px;
top : -25px;
left : 0px;
}
(23)css无限动画设置
animation-iteration-count : infinite;
(24)图片自适应设备尺寸
max-width : 100%;
height : auto;
(25)H5画布canvas文字垂直居中对齐
let ctx = document. querySelector ( "canvas" ) . getContext ( "2d" ) ;
ctx. textBaseline = 'middle' ;
ctx. textAlign = 'center' ;
(26)js 键盘上下左右 触发事件
document. onkeydown = function ( e) {
switch ( e. keyCode) {
case 37 :
console. log ( "左" ) ;
break ;
case 38 :
console. log ( "上" ) ;
break ;
case 39 :
console. log ( "右" ) ;
break ;
case 40 :
console. log ( "下" ) ;
break ;
}
}
(27)实现JS选择器选择的元素数组拥有filter函数功能
let nodeArr= document. querySelectorAll ( '选择器' ) ;
let re= Array. prototype. filter. call ( nodeArr, val=> 筛选条件) ;
(28)实现JS对象深拷贝功能
function copy ( target, source) {
for ( let key in source) {
if ( source[ key] == null || typeof source[ key] != "object" ) {
target[ key] = source[ key]
} else {
if ( source[ key] instanceof Array ) {
target[ key] = [ ... source[ key] ] ;
} else {
target[ key] = {
} ;
copy ( target[ key] , source[ key] ) ;
}
}
}
}
(29)解决Webpack打包工具下,js的函数无法直接在html中使用的情况
window. showNoticeNoRead = showNoticeNoRead;
(30)Vue-Cli中【分页】模块组件
//分页组件
< template>
< div class = " fenye" >
< div class = " pages" >
< button @click = " gotoFirstPage" > 首页</ button>
< a
href = " "
@click.prevent = " changePage(item)"
v-for = " item in pages"
:key = " item"
> {
{ item }}</ a
>
< a href = " " > ...</ a>
< button @click = " gotoLastPage" > 尾页</ button>
</ div>
< select name = " " id = " " v-model.number = " pageSize" >
< option > 5</ option>
< option > 10</ option>
< option > 15</ option>
</ select>
< div class = " message" >
< span> 一共{
{ pages }}页/当前{
{ currentPage }}页</ span>
</ div>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
pageSize: 5
}
} ,
props: [ "currentPage" , "pages" ] ,
methods: {
gotoFirstPage ( ) {
this . $emit ( "fenye" , 1 , this . pageSize)
} ,
gotoLastPage ( ) {
this . $emit ( "fenye" , this . pages, this . pageSize)
} ,
changePage ( page) {
this . $emit ( "fenye" , page, this . pageSize)
} ,
} ,
watch: {
pageSize ( ) {
this . $emit ( "fenye" , 1 , this . pageSize)
}
}
} ;
</ script>
< style scoped lang = ' scss' >
.fenye {
display : flex;
margin-top : 20px;
align-items : center;
.pages {
display : flex;
a {
display : block;
width : 30px;
height : 30px;
border : 1px solid gainsboro;
display : flex;
align-items : center;
justify-content : center;
text-decoration : none;
color : black;
}
}
}
</ style>