本篇文章主要給大家介紹一下如何使用html+css完成二級橫向以及豎向菜單導航制作;菜單導航是網站建設中最常用的一塊了,基本每個網站內都會有個導航菜單,用鼠標劃過還可以有下拉子菜單。
由上圖我們可以看出,該圖包含一個橫向導航條,然后鼠標經過橫向導航條之后,子導航顯示出來。
我們這里主要用到的知識點就是列表標簽(ul、dl)的使用、浮動(float)的使用、絕對定位(absolute)及鼠標經過(hover)的效果。
1、列表標簽ul、dl(我們使用ul、dl來創建同類型的導航元素內容,通過設置css樣式來達到圖片所示效果);
2、浮動元素float(每個導航元素我們需要使用float:left;讓其左對齊,這里涉及到了我們之前講解的如何清除浮動的影響);
3、絕對定位absolute(對于子導航我們要使用絕對定位來讓其浮動在上級有定位元素的下方,不占據元素空間)
4、鼠標經過hover(使用css的鼠標經過元素(hover)效果,配合display的none(隱藏)和block(顯示)來實現子菜單的顯示與隱藏)
具體的實現html代碼以及css代碼就如下圖所示:
還有一個縱向菜單導航原理跟橫向的類似,只需簡單調整一下css代碼即可。
html代碼跟橫向一樣,這里就不再貼圖,具體的實現圖片效果以及css代碼就如下圖所示:
好了,本篇文章就給大家說到這里,大家自己動手寫一下看能不能寫出一樣的頁面效果出來,也可以找一些類似的頁面自己練習一下,有需要源碼的可以直接私信我即可。
每日金句:每天收獲小進步,積累起來就是大進步;每天收獲小幸福,積攢起來便成大幸福。喜歡我的文章的小伙伴記得關注一下哦,每天將為你更新最新知識。
From 百度百科:甘特圖(Gantt chart)又稱為橫道圖、條狀圖(Bar chart)。其通過條狀圖來顯示項目,進度,和其他時間相關的系統進展的內在關系隨著時間進展的情況。
yarn add dhtmlx-gantt
復制代碼
import 'dhtmlx-gantt';
import 'dhtmlx-gantt/codebase/dhtmlxgantt.css';
import 'dhtmlx-gantt/codebase/ext/dhtmlxgantt_marker.js';
import 'dhtmlx-gantt/codebase/ext/dhtmlxgantt_tooltip.js';
import 'dhtmlx-gantt/codebase/locale/locale_cn.js';
import * as React from 'react';
import { getGanttConfigByZoomValue } from '../../utils/milestone.lib';
import Toolbar from './components/Toolbar';
import * as styles from './index.module.less';
export default class Gantt extends React.Component<any> {
state = {
currentZoom: 'Days', // 默認按日維度展示
isMount: false,
};
private ganttContainer: any;
componentWillReceiveProps (nextProps: any) {
this.generateGantt();
this.setState({ isMount: true });
}
handleZoomChange = (zoom: string) => {
this.setState({ currentZoom: zoom }, () => {
this.generateGantt();
});
}
async generateGantt () {
const { ganttData } = this.props;
if (this.state.isMount) { // 若不加判斷,首次使用會報錯
gantt.clearAll(); // 移除所有任務,否則更新時任務會疊加
}
this.setConfig(); // 添加配置
gantt.init(this.ganttContainer); // 初始化 dhtmlxGantt 到 ganttContainer 容器中
gantt.parse(ganttData); // 將數據注入到甘特圖
}
setConfig () {
...
}
setZoom (value: string) {
gantt.config = {
...gantt.config,
...getGanttConfigByZoomValue(value), // 根據維度展示不同的日期格式
};
}
renderContent () {
const { currentZoom } = this.state;
return (
<React.Fragment>
<div className={styles.zoomBar}>
<Toolbar zoom={currentZoom} onZoomChange={this.handleZoomChange} />
</div>
<div className={styles.gantt}}>
<div
ref={input => {
this.ganttContainer = input;
}}
style={{ width: '100%', height: '100%' }}
/>
</div>
</React.Fragment>
);
}
render () {
return (
<div className={styles.ganttWrapper}>
{this.renderContent()}
</div>
);
}
}
復制代碼
gantt.config.readonly = true;
gantt.init(this.ganttContainer);
復制代碼
gantt.config.readonly = true; // 開啟只讀模式
select_task: false, // 禁止任務被選中,
gantt.init(this.ganttContainer);
復制代碼
gantt.config.columns = [
{
name: 'text',
label: '里程碑節點',
width: 280,
template: function (obj: any) {
return `節點:${obj.text}`; // 通過 template 回調可以指定返回內容值
},
},
];
gantt.init(this.ganttContainer);
復制代碼
// 自定義tooltip內容
gantt.templates.tooltip_text = function (start: Date, end: Date, task: any) {
const t = gantt;
const output = `<b>里程碑:</b>${task.text}<br/><b>狀態:</b>${
MILESTONE_STATE_MAP[task.state]
}<br/><b>計劃開始時間:</b>${t.templates.tooltip_date_format(
start,
)}<br/><b>計劃結束時間:</b>${t.templates.tooltip_date_format(end)}`;
return output;
},
// 添加tooltip
gantt.attachEvent('onGanttReady', function () {
var tooltips = gantt.ext.tooltips;
tooltips.tooltip.setViewport((gantt as any).$task_data);
});
復制代碼
const { online_date, offline_date } = this.props;
// 今日紅線
let today = new Date(`${getEndOfDate()}`); // getEndOfDate 為獲取今天結束時間的方法
gantt.addMarker({
start_date: today,
css: 'today',
text: '今日',
});
// 項目開始時間
if (online_date) {
gantt.addMarker({
start_date: online_date,
css: 'projectStartDate',
text: '項目開始',
});
}
// 項目結束時間
if (offline_date) {
gantt.addMarker({
start_date: offline_date,
css: 'projectEndDate',
text: '項目結束',
});
}
復制代碼
.projectStartDate, .projectEndDate {
background: #00bcd4;
}
復制代碼
task_class: function (start: Date, end: Date, task: any) {
return `milestone-${task.state}`; // task.state值為default/unfinished/finished/canceled其中一種
},
復制代碼
.milestone-default {
border: none;
background: rgba(0, 0, 0, 0.45);
}
.milestone-unfinished {
border: none;
background: #5692f0;
}
.milestone-finished {
border: none;
background: #84bd54;
}
.milestone-canceled {
border: none;
background: #da645d;
}
復制代碼
// 突出周末顏色
(gantt.templates as any).timeline_cell_class = function (item: any, date: Date): string {
if (date.getDay() === 0 || date.getDay() === 6) {
return 'weekend';
}
return '';
};
復制代碼
// 突出周末顏色
const disableHighlight =
this.state.currentZoom === 'Years' || this.state.currentZoom === 'Months';
(gantt.templates as any).timeline_cell_class = function (item: any, date: Date): string {
if (!disableHighlight && (date.getDay() === 0 || date.getDay() === 6)) {
return 'weekend';
}
return '';
};
復制代碼
export function getGanttConfigByZoomValue (value: string) {
switch (value) {
case 'Hours':
return {
scale_unit: 'day',
scale_height: 50,
min_column_width: 30,
date_scale: '%Y-%m-%d',
subscales: [
{
unit: 'hour',
step: 1,
date: '%H',
},
],
};
case 'Days':
return {
scale_unit: 'week',
scale_height: 50,
min_column_width: 70,
date_scale: '%Y年 %W周',
subscales: [
{
unit: 'day',
step: 1,
date: '%m-%d',
},
],
};
case 'Months':
return {
scale_unit: 'month',
scale_height: 50,
min_column_width: 70,
date_scale: '%Y-%m',
subscales: [
{
unit: 'week',
step: 1,
date: '%W周',
},
],
};
case 'Years':
return {
scale_unit: 'year',
scale_height: 50,
min_column_width: 70,
date_scale: '%Y年',
subscales: [
{
unit: 'month',
step: 1,
date: '%M',
},
],
};
default:
return {};
}
}
復制代碼
以上內容如有遺漏錯誤,歡迎留言 ??指出,一起進步
如果覺得本文對你有幫助,留下你寶貴的
轉載鏈接:https://juejin.im/post/5e7ffd56f265da794e526102
這里是云端源想IT,幫你輕松學IT”
嗨~ 今天的你過得還好嗎?
睡眠等同于希望
每次醒來都是一個新的開始
一個新的希望
- 2024.03.22 -
在Web開發的世界中,CSS(層疊樣式表)是構建視覺吸引力和定義網頁布局的不可或缺的工具。
掌握如何恰當地引入CSS樣式以及理解它們的優先級規則,對于前端開發者來說至關重要。今天,我們就來深入探討CSS的四種引入方式,以及選擇器的優先級之謎,了解常用的CSS樣式及使用方法!
CSS(層疊樣式表)為網頁提供了豐富的樣式定義,允許開發者通過多種方式將樣式應用到HTML文檔中。以下是四種主要的CSS引入方式:
1.1 行內樣式
這是最直接也最簡單的方法,通過在HTML元素的style屬性中直接編寫CSS規則。
示例:
<p style="color: red; font-size: 20px;">這是一段紅色的文字。</p>
這種方式的優點是簡單快捷,但缺點是它使得HTML代碼與樣式混合,不夠純凈,且不利于樣式的復用和維護。
1.2 內嵌樣式
在一個HTML文檔中使用<style>標簽將CSS規則嵌入到HTML的head部分。這種方式適用于定義特定于某一頁面的樣式。
示例:
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
1.3 外部樣式
這是最常用的方法,它通過<link>標簽將外部的CSS文件鏈接到HTML文檔中。這種方法的優勢在于可以在多個頁面間共享同一個樣式文件,有助于保持代碼的整潔和一致性。
示例:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
其中,mystyle.css的內容可能如下:
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
1.4 導入樣式
使用@import語句在CSS文件中導入另一個CSS文件。盡管這種方法可以分離樣式表,但它通常不被推薦使用,因為其加載時序可能會影響頁面渲染效率。
示例:
@import url('https://fonts.googleapis.com/css?family=Roboto');
body {
font-family: 'Roboto', sans-serif;
}
1.5 樣式單優先級
作用域范圍:外部樣式表>內部樣式表>行內樣式表
優先級:
2.1 字體樣式
normal - 文字正常顯示
italic - 文本以斜體顯示
oblique - 文本為“傾斜”(傾斜與斜體非常相似,但支持較少)
font-weight 屬性指定字體的粗細
示例:
<!DOCTYPE html>
<html>
<head>
<style>
.sp1{
color: darkorange;
font-size: 20px;
font-weight: bolder; /* bolder 字體是否加粗*/
font-style: italic; /* italic 字體是否傾斜*/
font-family: "宋體"; /* 設置字體樣式*/
}
.sp2{
/* 簡寫 */
/* 順序不能能變:style-weigth-size-family */
font:italic bolder 15px 宋體 ;
color:rgb(28, 235, 97);
}
</style>
<body>
<span>
編程學習,從云端源想開始!
</span><br>
<span>
讓知識觸手可及
</span>
<p>讓知識觸手可及</p>
</body>
</html>
2.2 文本樣式
color: 字體顏色
text-align: center; - - 文本對齊方式
text-decoration:none; - - 文本的線
text-shadow: pink 5px 5px 2px; - - 文本的陰影 【陰影顏色-水平方向的偏移量-垂直方向的偏移量-模糊距離】
line-height: - - 行高 (文本在標簽內所占的高度)
width:
height:
border: 1px #ffffff solid; - - 盒子邊框【邊框粗細-顏色-邊框線樣式】
示例:
<style>
.p{
color: rgb(0, 255, 21); /* 字體顏色 */
text-align: center; /* 文本對齊方式 */
text-decoration:none; /* 文本的線 */
text-shadow: pink 5px 5px 2px; /* 文本的陰影 【陰影顏色-水平方向的偏移量-垂直方向的偏移量-模糊距離】*/
line-height: 400px; /* 行高 (文本在標簽內所占的高度)*/
width: 400px;
height: 300px;
border: 1px rgb(76, 14, 223) solid; /* 盒子邊框【邊框粗細-顏色-邊框線樣式】 */
}
</style>
</head>
<body>
<p>歡迎來到云端源想!</p>
<a href="https://www.baidu.com"></a>
</body>
2.3 背景樣式
width: 500px;
height: 1200px;
background-color: pink; - - 背景顏色
background-image: url(…/img/background.jpg); - - 背景圖片
background-repeat: no-repeat; - - 背景圖片是否平鋪
background-position: left top; - - 指定背景圖片的位置
background-attachment: fixed; - - 背景圖片是否隨著標簽滾動 【fixed-固定 scroll-滾動】
示例:
<style>
.d{
width: 500px;
height: 1200px;
background-color: pink; /* 背景顏色 */
background-image: url(../img/background.jpg); /* 背景圖片 */
background-repeat: no-repeat; /* 背景圖片是否平鋪 */
background-position: left top; /* 指定背景圖片的位置 */
background-attachment: fixed; /* 背景圖片是否隨著標簽滾動 【fixed-固定 scroll-滾動】 */
}
</style>
</head>
<body>
<div>
</div>
2.4 列表樣式
<!DOCTYPE html>
<html>
<head>
<style>
li{
background-color: lemonchiffon;
/*列表樣式:常用取值:none-無樣式 square-正方形 circle-空心圓 decimal-數字*/
list-style-type: circle;
/*列表樣式為自定義圖片*/
list-style-image: url(../img/2.jpg);
/*列表樣式的放置位置*/
list-style-position: inside;
/*列表樣式縮寫*/
list-style: square url(../img/2.jpg) inside;
/*常用的列表樣式*/
list-style: none;
}
</style>
</head>
<body>
<ul>
<li>列表項1</li>
<li>列表項2</li>
<li>列表項3</li>
</ul>
</body>
</html>
2.5 邊框樣式
<!DOCTYPE html>
<html>
<head>
<style>
.border{
/*邊框寬度*/
border-width: 2px;
/*邊框顏色*/
border-color: red;
/*邊框樣式:solid 實線 dotted 點線 dashed 虛線*/
border-style: dashed;
/*邊框樣式縮寫:樣式 顏色 寬度*/
border:solid green 5px;
/*邊框可以為4個方向分別設置*/
border-top: dashed black 4px;
border-right: dashed #FF00FF 4px;
border-bottom: dotted darkblue 4px;
border-left: solid fuchsia 5px;
/*沒有邊框*/
border: none;
/*常用的細邊框樣式*/
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div class="border">這是一個帶有邊框的元素</div>
</body>
</html>
2.6 盒子模型
所有的html元素可以看做是盒子,在css中,"box model"是用來設計和布局時使用。
CSS盒子模型本質是一個盒子,封裝周圍的html元素,它包括:邊框、邊距、填充、實際內容。
盒子模型允許我們在其他元素和周圍元素邊框之間的空間放置元素。
想要快速入門前端開發嗎?推薦一個前端開發基礎課程,這個老師講的特別好,零基礎學習無壓力,知識點結合代碼,邊學邊練,可以免費試看試學,還有各種輔助工具和資料,非常適合新手!點這里前往學習哦!云端源想
示例:
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* border:邊框,分4個方向,同理margin、padding也分為四個方向
* margin:元素與元素之間對的距離
* padding:內容與邊框之間的距離
* 設置的時候順序:上 右 下 左
*/
.div{
border: solid red 10px;
/*四個方向上的元素與元素之間的距離都是50px*/
margin: 50px;
/*兩個值的時候:第一個參數表示上下距離都是50px,第二個參數表示左右距離都是100px*/
margin: 50px 100px;
padding: 50px;
/*
一個元素真正的寬度=width+左右padding值+左右的border值
一個元素的真正高度=height+上下的padding值+上下的border值
* */
}
</style>
</head>
<body>
<div>111111111112222222222223333333333333333</div>
</body>
1)盒子的寬高
元素的實際寬度和高度:
2)設置寬度=元素實際寬度,box-sizing屬性。
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* box-sizing:確認元素的大小
content-box: 實際寬度=width+左右的psdding值+上下的border值
實際高度=height+上下的padding值+上下的border值
border-box:實際寬度=width;實際高度=height
padding和border不會影響元素的實際寬高
* */
.box{
width: 100px;
height: 200px;
border: 5px solid;
padding: 5px;
box-sizing: content-box;
}
</style>
</head>
<body>
<div>你好中國</div>
</body>
CSS的世界博大精深,以上只是冰山一角,希望通過這些基礎的常用樣式可以幫助你快速進入CSS世界的大門。
掌握CSS的引入方式和選擇器優先級是構建高效、可維護網站的關鍵。通過這些知識,你可以更好地管理和優化你的樣式代碼,創造出既美觀又專業的網頁設計。現在,準備好邁入CSS的世界,開啟你的創意之旅吧!
我們下期再見!
END
文案編輯|云端學長
文案配圖|云端學長
內容由:云端源想分享
*請認真填寫需求信息,我們會在24小時內與您取得聯系。