主頁 > 前端設計 > 中國象棋(react hooks版)

中國象棋(react hooks版)

2021-10-02 10:00:21 前端設計

文章目錄

  • 前言
  • 功能展示
  • 新增模塊功能
    • 搭建專案框架
    • 配置React Router
    • 配置React Store
    • 配置i18n
    • 配置主題色
    • React影片
    • React ColorPicker
    • 打譜記錄
    • 單元測驗
  • 核心功能優化
    • 落子音效
    • dom解耦
    • 小結
  • 結語

前言

它終于來了,終于來了,在第一版的基礎上,歷時近兩個月終于完成react版本的中國象棋,這個版本使用的技術堆疊包括react hooks+ts+sass+antd(本來還準備使用redux,但是嫌麻煩,就沒用使用),這次主要是時間消耗是在hooks以及ts的使用上,畢竟從某種意義上來說,我也是ts和hooks的新手,所以這個專案也很適合新手來學習并使用hooks以及ts,
在正式開始講述之前,我建議大家可以先看看我上個使用原生js實作的版本,因為核心邏輯是幾乎是復用之前的代碼,原生js版本
同樣,想趕緊獲取原始碼跑起來試一試的朋友可以點擊此鏈接:原始碼地址
(創作不易,如果覺得還可以的話,那么就在github上留下一個Star)

功能展示

由于沒有部署在服務器上,就只能可以先給大家看幾張效果圖:
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
暫時就放這幾張圖,更多功能有待大家自己去體驗,
下面,我帶領大家具體從零開始去如何創建此專案,(講述主要流程)

新增模塊功能

搭建專案框架

我們需要創建基于ts的react專案,在這里我建議大家使用官方的腳手架進行創建:

npx create-react-app my-app --template typescript

創建完成之后,可以把一些默認的沒必要的檔案洗掉掉,
緊接著下一步就是配置路由,

配置React Router

這里我就不具體講解如何配置React Router,網上有很多學習資料,大家可以自行搜索學習,這里我給出一個當時我自己配置時參考的一個阮一峰老師的檔案:React Router使用檔案
在這里我們需要配置Home,Settings,Change以及About四個路由頁面,
在App組件主要是配置路由頁面的顯示:

const routes: Routers = [
  ["home", Home],
  ["settings", Settings],
  ["change", Change],
  ["about", About],
];
function App() {
  return (
    <Router>
      <div className="App">
        <AppMenu routers={routes} />
        <div id="pageContainer" className="page-container">
          <Switch>
            {routes.map(([label, Component]) => (
              <Route
                key={label as string}
                path={`/${label}`}
              >
                <Component />
              </Route>
            ))}
            <Route path="/" exact>
              <Home />
            </Route>
            <Route path="*"><h1>Page not found.</h1></Route>
          </Switch>
        </div>
      </div>
    </Router>
  );
}

在AppMenu組件中進行路由的切換:

export default function AppMenu(props: MenuProps) {
    const { t } = useTranslation();
    const [key, setKey] = useState('');
    const { routers } = props;
    const changeMenu = useCallback((e: any) => {
        setKey(e.key);
    }, []);
    const setContent = () => (
        <Menu onClick={changeMenu} selectedKeys={[key]}>
            {routers.map(([label]) => <Menu.Item key={label.toString()}>
                <Link to={`/${label}`}>{t(label.toString())}</Link>
            </Menu.Item>)}
        </Menu>
    )
    return (
        <>
            <Dropdown overlay={setContent} placement="bottomRight">
                <Button className={'app-menu'} icon={<AppstoreOutlined />} />
            </Dropdown>
        </>
    )
}

路由配置完成之后,我們就可以開始撰寫每個路由頁面的內容,
下面講述不區分步驟順序,

配置React Store

這個板塊更一般是情況下是使用redux,將store里面的資料交給redux進行管理,但是這里我選擇了自己管控(主要原因還是資料不多),有興趣的朋友可以基于此專案使用redux將其進一步升級,
setting.ts:(保存著Setting路由頁面里的資料狀態)

const settings = {
    isShowPoint: true,
    isUseSound: true,
    pointColor: '#1198bc',
    blackChessColor: '#000',
    redChessColor: 'red',
    isReverse: false,
    themeKeys: themes[0],
    language: languages.zh
};

home.ts:(保存著非Setting路由頁面但是需要使用的全域的資料狀態)

const home = {
    isStart: false,
    isEnd: true,
    isShowPoint: false,
    isShowPath: false,
    version: 'v1.0.0'
};

配置i18n

這個板塊是為了使專案語言能夠多樣化,這里我也是參考網上的博客進行配置學習,資源鏈接:配置 i18n
index.ts:

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
// 層級嵌套如何處理
const resources = {
    en: {
        translation: {
            'title': 'Chinese Chess',
            'settings': 'Settings',
            'start': 'START',
            'surrender': 'SURRENDER',
            'change': 'Change',
            'home': 'Home',
            'about': 'About',
            'lang': 'Language',
            'black chess color': 'Black Chess Color:',
            'red chess color': 'Red Chess Color:',
            'show point positions': 'Show Point Positions:',
            'use sound':'Use Sound:',
            'theme style': 'Theme Style:',
            'default theme': 'Default Theme',
            'dark theme': 'Dark Theme',
            'point color': 'Point Color:',
            'reverse board': 'Reverse Board:',
            'reverse rule': 'This operation can only be used before the start of the game',
            'chooseOffensiveTitle': 'Choose Offensive',
            'chooseOffensiveBody': 'Who is to go on the offensive?',
            'me': 'Me',
            'computer': 'Computer',
            'game start': 'Game Start',
            'red win': 'Red Win',
            'black win': 'Black Win',
            'giveTitle': 'Give up?',
            'giveBody': 'Are you sure to give up?',
            'ok': 'OK',
            'cancel': 'Cancel',
            'show': 'Show',
            'rounds': 'Rounds:'
        }
    },
    zh: {
        translation: {
            'title': '中國象棋',
            'settings': '設定',
            'start': '開始',
            'surrender': '認輸',
            'change': '更新',
            'home': '首頁',
            'about': '關于',
            'lang': '語言',
            'black chess color': '黑方棋子顏色:',
            'red chess color': '紅方棋子顏色:',
            'show point positions': '顯示落子提示點:',
            'use sound':'使用音效:',
            'theme style': '主題樣式:',
            'default theme': '默認主題',
            'dark theme': '暗黑主題',
            'point color': '提示點顏色:',
            'reverse board': '交換棋盤位置:',
            'reverse rule': '此操作僅限于開局前使用',
            'chooseOffensiveTitle': '選擇先手',
            'chooseOffensiveBody': '誰執紅方?',
            'me': '我',
            'computer': '電腦',
            'game start': '游戲開始',
            'red win': '紅方贏',
            'black win': '黑方贏',
            'giveTitle': '認輸?',
            'giveBody': '你確定認輸嗎?',
            'ok': '確認',
            'cancel': '取消',
            'show': '查看',
            'rounds': '回合數:'
        }
    },
};
i18n.use(initReactI18next).init({
    resources,
    lng: 'zh', //設定當前語言
    keySeparator: false, // we do not use keys in form messages.welcome
    interpolation: {
        escapeValue: false // react already safes from xss
    }
});
export default i18n;

配置主題色

這里我是基于Sass進行主題色配置,至于如何使用Sass實作主題切換大家可以參看這篇文章:使用Sass實作主題切換
theme.scss:

$default-body-bg: #fff;
$default-board-opacity: 1;
$default-ui-bg: #fff;
$default-switch-bg: #00000040;
$default-select-dropdown-bg: #fff;
$default-select-item-bg: #e6f7ff;
$default-select-active-item-bg: #f5f5f5;
$default-body-color: #000000d9;
$default-text-color: rgba(0, 0, 0, 0.45);
$default-btn-hover-color: #3c9be8;
$default-btn-border-color: #d9d9d9;
$default-btn-hover-border-color: #3c9be8;
$default-title-color: #09bb07;
$default-divider-border-color: rgba(0, 0, 0, 0.06);
$default-select-border-color: #d9d9d9;
$default-select-arrow-color: #00000040;
$dark-body-bg: #000;
$dark-board-opacity: 0.8;
$dark-select-dropdown-bg: #1f1f1f;
$dark-select-item-bg: #111b26;
$dark-select-active-item-bg: rgba(255, 255, 255, 0.08);
$dark-ui-bg: rgba(255, 255, 255, 0.8);
$dark-switch-bg: rgba(255, 255, 255, 0.3);
$dark-body-color: rgba(255, 255, 255, 0.65);
$dark-text-color: rgba(255, 255, 255, 0.45);
$dark-btn-hover-color: #165996;
$dark-btn-border-color: #434343;
$dark-btn-hover-border-color: #165996;
$dark-title-color: #065705;
$dark-divider-border-color: #303030;
$dark-select-border-color: #434343;
$dark-select-arrow-color: rgba(255, 255, 255, 0.3);

$themes: (
    default: (
        body-bg: $default-body-bg,
        board-opacity: $default-board-opacity,
        ui-bg: $default-ui-bg,
        switch-bg: $default-switch-bg,
        select-dropdown-bg: $default-select-dropdown-bg,
        select-item-bg: $default-select-item-bg,
        select-active-item-bg: $default-select-active-item-bg,
        body-color: $default-body-color,
        btn-hover-color: $default-btn-hover-color,
        btn-border-color: $default-btn-border-color,
        btn-hover-border-color: $default-btn-hover-border-color,
        title-color: $default-title-color,
        divider-border-color: $default-divider-border-color,
        select-border-color: $default-select-border-color,
        select-arrow-color: $default-select-arrow-color,
    ),
    dark: (
        body-bg: $dark-body-bg,
        board-opacity: $dark-board-opacity,
        ui-bg: $dark-ui-bg,
        switch-bg: $dark-switch-bg,
        select-dropdown-bg: $dark-select-dropdown-bg,
        select-item-bg: $dark-select-item-bg,
        select-active-item-bg: $dark-select-active-item-bg,
        body-color: $dark-body-color,
        text-color: $dark-text-color,
        btn-hover-color: $dark-btn-hover-color,
        btn-border-color: $dark-btn-border-color,
        btn-hover-border-color: $dark-btn-hover-border-color,
        title-color: $dark-title-color,
        divider-border-color: $dark-divider-border-color,
        select-border-color: $dark-select-border-color,
        select-arrow-color: $dark-select-arrow-color,
    ),
);

大家應該發現了這里面我不僅配置了一些基本標簽的主題色,并且還配置了antd組件內部的主題色,
themify.scss:

@import "./theme.scss";

@mixin themify {
    @each $theme-name, $theme-map in $themes {
        $theme-map: $theme-map !global;
        body[data-theme="#{$theme-name}"] & {
            @content;
        }
    }
}

@function themed($key) {
    @return map-get($theme-map, $key);
}

@each $theme-name, $theme-map in $themes {
    body[data-theme="#{$theme-name}"] {
        color: map-get($theme-map, "body-color");
        background-color: map-get($theme-map, "body-bg");
    }
}

由于body本身的主題色是不受屬性選擇器的影響,所以這里我將其單獨拎出來配置主題色,
使用案例:

h4.ant-typography,
.ant-typography h4 {
    @include themify {
        color: themed("body-color");
    }
}

.ant-divider {
    @include themify {
        border-top-color: themed("divider-border-color");
    }
}

.ant-typography.ant-typography-secondary {
    @include themify {
        color: themed("text-color");
    }
}

React影片

在此專案中我撰寫了一個react影片組件BigText,它是基于react-transition-group的CSSTransition組件進行實作,大家可以參考以下資源進行學習:CSSTransition官網 、CSSTransition知乎
BigText.tsx:

import { CSSTransition } from 'react-transition-group';
import { BigTextProps } from '../views/Home';

export default function BigText(props: BigTextProps) {
    const { text, isIn } = props;
    return (
        <>
            <CSSTransition classNames="fade" timeout={500} in={isIn} unmountOnExit>
                <div className="big-text">{text}</div>
            </CSSTransition>
        </>
    )
}

React ColorPicker

在此專案中我封裝的ColorPicker是基于React Color的ChromePicker,有關React Color的使用檔案可以點擊下面的資源:React Color檔案
ColorPicker.tsx:

import React, { useState, useCallback, CSSProperties } from "react";
import { Button } from 'antd';
import { BgColorsOutlined } from '@ant-design/icons';
import { ChromePicker } from 'react-color';
import { ColorPickProps } from "../views/Settings";

const popover: CSSProperties = {
    position: 'absolute',
    zIndex: 2,
    right: '0',
    top: '32px'
}
const cover: CSSProperties = {
    position: 'fixed',
    top: '0px',
    right: '0px',
    bottom: '0px',
    left: '0px',
}
const buttonStyle: CSSProperties = {
    position: 'relative'
}
export default function ColorPicker(props: ColorPickProps) {
    const { color, colorChange } = props;
    const [show, setShow] = useState(false);
    const changeShow = useCallback(() => {
        setShow(!show);
    }, [show]);
    const handleClose = useCallback(() => {
        setShow(false);
    }, []);
    return (
        <div style={buttonStyle}>
            <Button shape='circle' icon={<BgColorsOutlined />} onClick={changeShow} />
            {show ?
                <div style={popover}>
                    <div style={cover} onClick={handleClose} />
                    <ChromePicker color={color} onChange={colorChange} />
                </div>
                : null}
        </div>
    )
}

打譜記錄

打譜記錄得到的結果是通過chessRecord函式得到的,只需要知道當前棋子的開始位置、結束位置以及當前棋盤的狀態(是否反轉) 就能得到該棋子的此次打譜結果,
chessRecord.ts:

import { BoardCore } from "./board";
import role from "./role";
import texts from './text';

const redTexts = ['九', '八', '七', '六', '五', '四', '三', '二', '一'];
const blackTexts = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
const actions = ['平', '進', '退'];

// 車炮兵帥一類
// 馬士相一類
export function chessRecord(startPosition: number[], endPosition: number[], targetPosition: number[], player: BoardCore) {
    const { type, text } = player;
    const [redStart, blackStart] = targetPosition;
    let action = '';
    let res = '';
    if (type === role.red) {
        if (startPosition[0] > endPosition[0]) {
            action = redStart === 0 ? actions[2] : actions[1];
            res = (text === texts.redCar || text === texts.redGun || text === texts.redSoldier || text === texts.redGeneral)
                ? `${text}${redTexts[startPosition[1]]}${action}${redTexts[redTexts.length - (startPosition[0] - endPosition[0])]}`
                : `${text}${redTexts[startPosition[1]]}${action}${redTexts[endPosition[1]]}`;
        } else if (startPosition[0] < endPosition[0]) {
            action = redStart === 0 ? actions[1] : actions[2];
            res = (text === texts.redCar || text === texts.redGun || text === texts.redSoldier || text === texts.redGeneral)
                ? `${text}${redTexts[startPosition[1]]}${action}${redTexts[redTexts.length - (endPosition[0] - startPosition[0])]}`
                : `${text}${redTexts[startPosition[1]]}${action}${redTexts[endPosition[1]]}`;
        } else {
            action = actions[0];
            res = `${text}${redTexts[startPosition[1]]}${action}${redTexts[endPosition[1]]}`;
        }
    } else if (type === role.black) {
        if (startPosition[0] > endPosition[0]) {
            action = blackStart === 0 ? actions[2] : actions[1];
            res = (text === texts.blackCar || text === texts.blackGun || text === texts.blackSoldier || text === texts.blackGeneral)
                ? `${text}${blackTexts[startPosition[1]]}${action}${blackTexts[startPosition[0] - endPosition[0] - 1]}`
                : `${text}${blackTexts[startPosition[1]]}${action}${blackTexts[endPosition[1]]}`;
        } else if (startPosition[0] < endPosition[0]) {
            action = blackStart === 0 ? actions[1] : actions[2];
            res = (text === texts.blackCar || text === texts.blackGun || text === texts.blackSoldier || text === texts.blackGeneral)
                ? `${text}${blackTexts[startPosition[1]]}${action}${blackTexts[endPosition[0] - startPosition[0] - 1]}`
                : `${text}${blackTexts[startPosition[1]]}${action}${blackTexts[endPosition[1]]}`;
        } else {
            action = actions[0];
            res = `${text}${blackTexts[startPosition[1]]}${action}${blackTexts[endPosition[1]]}`;
        }
    } else {

    }
    return res;
}

單元測驗

由于還未撰寫ai演算法,這里只給打譜函式以及絕殺函式撰寫單元測驗,
chessRecord.test.ts:

import Gun from '../tools/gun';
import Horse from '../tools/horse';
import role from '../tools/role';
import text from '../tools/text';
import { chessRecord } from "../tools/chessRecord";
const blackHorse = new Horse(text.blackHorse, role.black);
const redGun = new Gun(text.redGun, role.red);
describe('測驗棋譜', () => {
    it('紅炮平動', () => {
        expect(chessRecord([7, 1], [7, 4], [9, 0], redGun)).toEqual("炮八平五");
    });
    it('黑炮前進', () => {
        expect(chessRecord([0, 1], [2, 2], [9, 0], blackHorse)).toEqual("馬2進3");
    });
});

核心功能優化

落子音效

由于在react中使用audio標簽參考本地音頻檔案很麻煩,所以這里我選擇參考線上的音頻并且使用js動態創建,
playSound.ts:

import settings from "../store/setting";

const url = 'https://www.xiangqiqipu.com/Scripts/game/sounds/';
type SoundName = 'click' | 'move' | 'check' | 'capture' | 'move2' | 'check2' | 'capture2';
export function playSound(name: SoundName) {
    if (settings.isUseSound) {
        new Audio(`${url}${name}.wav`).play();
    }
}

dom解耦

在board.ts中記錄棋盤的各種功能函式與屬性,但是所有有關棋盤dom的繪制全部都在Board.tsx中,
dom解耦沒有想象中的那么簡單,這里面有兩個很棘手的問題: board.ts中如何通知Board.tsx進行重新渲染,如何利用React渲染機制能夠精準控制棋子的渲染(保證棋子流暢的運動特效),

board.ts中如何通知Board.tsx進行重新渲染:這里我采用的方案是回呼函式+useState實作,
核心代碼如下:

 const [isRender, setIsRender] = useState(false);
    useEffect(() => {
        chessBoard.render = () => {
            setIsRender(!isRender);
        };
    }, [chessBoard, isRender]);

給board系結回呼函式,在board每次執行render方法會觸發Board.tsx的setIsRender進而重新渲染當前組件,
如果大佬有更好實作的方式,歡迎留言,

如何利用React渲染機制能夠精準控制棋子的渲染:由于每次跟新棋盤,所有的棋子都要全部重繪,但是實際上我們只操作了一個或者兩個棋子的位置,那么勢必會造成視覺上的影片錯誤,解決方案也很簡單,根據react的渲染對比渲染機制,我們只需要在每次輸出棋子串列前按照固定的順序進行排序即可保證react只重新渲染改變的棋子,
核心代碼如下:

  const chesses = useMemo(() => {
        const board = chessBoard.idBoard;
        const res = [];
        for (let i = 0; i < board.length; i++) {
            for (let j = 0; j < board[0].length; j++) {
                const chess = chessBoard.board[i][j];
                const id = board[i][j];
                if (chess === role.empty) {
                    continue;
                }
                const style: React.CSSProperties = {
                    color: chess.type === 'red' ? settings.redChessColor : settings.blackChessColor,
                    marginTop: `${1 + (11.49 * i)}%`,
                    marginLeft: `${0.5 + (11.56 * j)}%`
                };
                res.push({
                    className: `chess ${id === chessBoard.chessSelectId ? 'border' : ''}`,
                    id: id.toString(),
                    style,
                    key: id,
                    text: chess.text,
                });
            }
        }
        // 保證react更精準的更新棋子dom
        res.sort((a, b) => a.key - b.key);
        // console.log(res);
        return res;
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [chessBoard, isRender]);

Board.tsx完整代碼:

import React, { useEffect, useMemo, useState } from "react";
import { BoardProps } from '../views/Home';
import settings from "../store/setting";
import Point from "./Point";
import home from "../store/home";
import ChessRecord from "./ChessRecord";
import role from "../tools/role";
import { TextValue } from "../tools/text";

export type PointProps = {
    positions: number[][]
};

export type ChessRecordProps = {
    isShowPath: boolean;
    dataList: string[];
    changeShowPath: (flag: boolean) => void;
};

type ChessOptions = {
    className: string;
    id: string;
    style: React.CSSProperties;
    key: number;
    text: TextValue;
}

export default function Board(props: BoardProps) {
    const { isStart, chessBoard, isShowPath, changeShowPath } = props;
    const [isRender, setIsRender] = useState(false);
    useEffect(() => {
        chessBoard.render = () => {
            setIsRender(!isRender);
        };
    }, [chessBoard, isRender]);
    const chesses = useMemo(() => {
        const board = chessBoard.idBoard;
        const res = [];
        for (let i = 0; i < board.length; i++) {
            for (let j = 0; j < board[0].length; j++) {
                const chess = chessBoard.board[i][j];
                const id = board[i][j];
                if (chess === role.empty) {
                    continue;
                }
                const style: React.CSSProperties = {
                    color: chess.type === 'red' ? settings.redChessColor : settings.blackChessColor,
                    marginTop: `${1 + (11.49 * i)}%`,
                    marginLeft: `${0.5 + (11.56 * j)}%`
                };
                res.push({
                    className: `chess ${id === chessBoard.chessSelectId ? 'border' : ''}`,
                    id: id.toString(),
                    style,
                    key: id,
                    text: chess.text,
                });
            }
        }
        // 保證react更精準的更新棋子dom
        res.sort((a, b) => a.key - b.key);
        // console.log(res);
        return res;
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [chessBoard, isRender]);
    return (
        <div id='board' className="board" onClick={(e) => chessBoard.clickHandler(e)}>
            {isStart ? chesses.map((option: ChessOptions) => (
                <div className={option.className} id={option.id} style={option.style} key={option.key}>{option.text}</div>
            )) : <></>}
            {home.isShowPoint && isStart && settings.isShowPoint ? <Point positions={chessBoard.positions} /> : <></>}
            <ChessRecord isShowPath={isShowPath} changeShowPath={changeShowPath} dataList={chessBoard.chessRecords} />
        </div>
    );
}

小結

更多有關棋盤以及棋子設計的講述可參看上個版本的文章,

結語

到此,有關此版本的核心功能就介紹完畢,更多細節只能在原始碼中進行體驗,如果對此專案有任何疑問,歡迎評論區留言或者私信我,如果有想法為其添加一些其它新功能或者發現bug也歡迎留言,接下來,我應該會繼續進展ai模塊,下期再會!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/304782.html

標籤:其他

上一篇:解決vue的所有相關問題集合

下一篇:vue.js基礎

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • vue移動端上拉加載

    可能做得過于簡單或者比較low,請各位大佬留情,一起探討技術 ......

    uj5u.com 2020-09-10 04:38:07 more
  • 優美網站首頁,頂部多層導航

    一個個人用的瀏覽器首頁,可以把一下常用的網站放在這里,平常打開會比較方便。 第一步,HTML代碼 <script src=https://www.cnblogs.com/szharf/p/"js/jquery-3.4.1.min.js"></script> <div id="navigate"> <ul> <li class="labels labels_1"> ......

    uj5u.com 2020-09-10 04:38:47 more
  • 頁面為要加<!DOCTYPE html>

    最近因為寫一個js函式,需要用到$(window).height(); 由于手寫demo的時候,過于自信,其實對前端方面的認識也不夠體系,用文本檔案直接敲出來的html代碼,第一行沒有加上<!DOCTYPE html> 導致了$(window).height();的結果直接是整個document的高 ......

    uj5u.com 2020-09-10 04:38:52 more
  • WordPress網站程式手動升級要做好資料備份

    WordPress博客網站程式在進行升級前,必須要做好網站資料的備份,這個問題良家佐言是遇見過的;在剛開始接觸WordPress博客程式的時候,因為升級問題和博客網站的修改的一些嘗試,良家佐言是吃盡了苦頭。因為購買的是西部數碼的空間和域名,每當佐言把自己的WordPress博客網站搞到一塌糊涂的時候 ......

    uj5u.com 2020-09-10 04:39:30 more
  • WordPress程式不能升級為5.4.2版本的原因

    WordPress是一款個人博客系統,受到英文博客愛好者和中文博客愛好者的追捧,并逐步演化成一款內容管理系統軟體;它是使用PHP語言和MySQL資料庫開發的,用戶可以在支持PHP和MySQL資料庫的服務器上使用自己的博客。每一次WordPress程式的更新,就會牽動無數WordPress愛好者的心, ......

    uj5u.com 2020-09-10 04:39:49 more
  • 使用CSS3的偽元素進行首字母下沉和首行改變樣式

    網頁中常見的一種效果,首字改變樣式或者首行改變樣式,效果如下圖。 代碼: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, ......

    uj5u.com 2020-09-10 04:40:09 more
  • 關于a標簽的講解

    什么是a標簽? <a> 標簽定義超鏈接,用于從一個頁面鏈接到另一個頁面。 <a> 元素最重要的屬性是 href 屬性,它指定鏈接的目標。 a標簽的語法格式:<a href=https://www.cnblogs.com/summerxbc/p/"指定要跳轉的目標界面的鏈接">需要展示給用戶看見的內容</a> a標簽 在所有瀏覽器中,鏈接的默認外觀如下: 未被訪問的鏈接帶 ......

    uj5u.com 2020-09-10 04:40:11 more
  • 前端輪播圖

    在需要輪播的頁面是引入swiper.min.js和swiper.min.css swiper.min.js地址: 鏈接:https://pan.baidu.com/s/15Uh516YHa4CV3X-RyjEIWw 提取碼:4aks swiper.min.css地址 鏈接:https://pan.b ......

    uj5u.com 2020-09-10 04:40:13 more
  • 如何設定html中的背景圖片(全屏顯示,且不拉伸)

    1 <style>2 body{background-image:url(https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture); 3 background-size:cover;background ......

    uj5u.com 2020-09-10 04:40:16 more
  • Java學習——HTML詳解(上)

    HTML詳解 初識HTML Hyper Text Markup Language(超文本標記語言) 1 <!--DOCTYPE:告訴瀏覽器我們要使用什么規范--> 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <!--meta 描述性的標簽,描述一些 ......

    uj5u.com 2020-09-10 04:40:33 more
最新发布
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 07:59:23 more
  • 生產事故-走近科學之消失的JWT

    入職多年,面對生產環境,盡管都是小心翼翼,慎之又慎,還是難免捅出簍子。輕則滿頭大汗,面紅耳赤。重則系統停擺,損失資金。每一個生產事故的背后,都是寶貴的經驗和教訓,都是專案成員的血淚史。為了更好地防范和遏制今后的各類事故,特開此專題,長期更新和記錄大大小小的各類事故。有些是親身經歷,有些是經人耳傳口授 ......

    uj5u.com 2023-04-18 07:55:04 more
  • 記錄--Canvas實作打飛字游戲

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 打開游戲界面,看到一個畫面簡潔、卻又富有挑戰性的游戲。螢屏上,有一個白色的矩形框,里面不斷下落著各種單詞,而我需要迅速地輸入這些單詞。如果我輸入的單詞與螢屏上的單詞匹配,那么我就可以獲得得分;如果我輸入的單詞錯誤或者時間過長,那么我就會輸 ......

    uj5u.com 2023-04-04 08:35:30 more
  • 了解 HTTP 看這一篇就夠

    在學習網路之前,了解它的歷史能夠幫助我們明白為何它會發展為如今這個樣子,引發探究網路的興趣。下面的這張圖片就展示了“互聯網”誕生至今的發展歷程。 ......

    uj5u.com 2023-03-16 11:00:15 more
  • 藍牙-低功耗中心設備

    //11.開啟藍牙配接器 openBluetoothAdapter //21.開始搜索藍牙設備 startBluetoothDevicesDiscovery //31.開啟監聽搜索藍牙設備 onBluetoothDeviceFound //30.停止監聽搜索藍牙設備 offBluetoothDevi ......

    uj5u.com 2023-03-15 09:06:45 more
  • canvas畫板(滑鼠和觸摸)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>canves</title> <style> #canvas { cursor:url(../images/pen.png),crosshair; } #canvasdiv{ bo ......

    uj5u.com 2023-02-15 08:56:31 more
  • 手機端H5 實作自定義拍照界面

    手機端 H5 實作自定義拍照界面也可以使用 MediaDevices API 和 <video> 標簽來實作,和在桌面端做法基本一致。 首先,使用 MediaDevices.getUserMedia() 方法獲取攝像頭媒體流,并將其傳遞給 <video> 標簽進行渲染。 接著,使用 HTML 的 < ......

    uj5u.com 2023-01-12 07:58:22 more
  • 記錄--短視頻滑動播放在 H5 下的實作

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 短視頻已經無數不在了,但是主體還是使用 app 來承載的。本文講述 H5 如何實作 app 的視頻滑動體驗。 無聲勝有聲,一圖頂百辯,且看下圖: 網址鏈接(需在微信或者手Q中瀏覽) 從上圖可以看到,我們主要實作的功能也是本文要講解的有: ......

    uj5u.com 2023-01-04 07:29:05 more
  • 一文讀懂 HTTP/1 HTTP/2 HTTP/3

    從 1989 年萬維網(www)誕生,HTTP(HyperText Transfer Protocol)經歷了眾多版本迭代,WebSocket 也在期間萌芽。1991 年 HTTP0.9 被發明。1996 年出現了 HTTP1.0。2015 年 HTTP2 正式發布。2020 年 HTTP3 或能正... ......

    uj5u.com 2022-12-24 06:56:02 more
  • 【HTML基礎篇002】HTML之form表單超詳解

    ??一、form表單是什么

    ??二、form表單的屬性

    ??三、input中的各種Type屬性值

    ??四、標簽 ......

    uj5u.com 2022-12-18 07:17:06 more