溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

css創(chuàng)建3D立體條形圖的方法

發(fā)布時間:2020-08-31 10:57:20 來源:億速云 閱讀:168 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)css創(chuàng)建3D立體條形圖的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

示例代碼在WebKit瀏覽器中效果最好,在Firefox(v13)中也相當(dāng)不錯。

1、設(shè)置網(wǎng)格

首先設(shè)置一個#stage元素,我們可以在其中定義將要查看任何3D轉(zhuǎn)換的透視圖 。 基本上是查看器與平面屏幕相關(guān)的位置。然后,因為我們正在創(chuàng)建圖形,我們需要設(shè)置軸和網(wǎng)格(#chart)。

雖然我們可以輕松地創(chuàng)建背景圖像并將其平鋪以形成網(wǎng)格圖案,但我們決定使用CSS 線性漸變語法。在下面的所有代碼中,-moz- styles只復(fù)制  -webkit-樣式。

<style type="text/css">

  #stage {
    -webkit-perspective: 1200px;
    -webkit-perspective-origin: 0% 0%;
    -moz-perspective: 1200px;
    -moz-perspective-origin: 0% 0%;
    background: rgba(0,255,255,0.2);
  }
  #chart {
    position: relative;
    margin: 10em auto;
    width: 400px;
    height: 160px;
    border: 1px solid #000;
    background: -webkit-repeating-linear-gradient(left, rgba(0,0,0,0) 0, rgba(0,0,0,0) 38px, #ccc 40px), -webkit-repeating-linear-gradient(bottom, rgba(0,0,0,0), rgba(0,0,0,0) 38px, #ccc 40px);
    background: -moz-repeating-linear-gradient(left, rgba(0,0,0,0) 0, rgba(0,0,0,0) 38px, #ccc 40px), -moz-repeating-linear-gradient(bottom, rgba(0,0,0,0), rgba(0,0,0,0) 38px, #ccc 40px);
    -webkit-transform-origin: 50% 50%;
    -webkit-transform: rotateX(65deg);
    -webkit-transform-style: preserve-3d;
    -moz-transform-origin: 50% 50%;
    -moz-transform: rotateX(65deg);
    -moz-transform-style: preserve-3d;
  }

</style>

圖表大小為400 x 160像素,網(wǎng)格為40像素。如您所見,背景網(wǎng)格由兩個水平和垂直運(yùn)行的重復(fù)漸變組成。圖表已從屏幕傾斜65度。

2、定義3D條形圖

圖表中的每個條形圖都由四個邊和一個帽組成。這里的樣式是針對條形 CSS類,然后可以在不同的位置和顏色中多次使用。它們在HTML中定義,您很快就會看到。

要想象正在應(yīng)用的變換,請考慮頁面上的垂直十字平面。然后將四個側(cè)面旋轉(zhuǎn)離開我們以形成柱子。簡單。

<style type="text/css">

  .bar {
    position: absolute;
    bottom: 40px;
    margin: 0 4px;
    width: 32px;
    height: 40px;
    outline: 1px solid #000;
    text-align: center;
    line-height: 40px;
    -webkit-transform-style: preserve-3d;
    -moz-transform-style: preserve-3d;
    font-size: 20px;
  }
  .barfront, .barback, .barleft, .barright {
    position: absolute;
    outline: inherit;
    background: inherit;
  }
  .barfront {
    width: inherit;
    bottom: 0;
    -webkit-transform: rotateX(90deg);
    -webkit-transform-origin: 50% 100%;
    -moz-transform: rotateX(90deg);
    -moz-transform-origin: 50% 100%;
  }
  .barback {
    width: inherit;
    top: 0;
    -webkit-transform: rotateX(-90deg);
    -webkit-transform-origin: 50% 0;
    -moz-transform: rotateX(-90deg);
    -moz-transform-origin: 50% 0;
  }
  .barright {
    height: inherit;
    right: 0;
    -webkit-transform: rotateY(-90deg);
    -webkit-transform-origin: 100% 50%;
    -moz-transform: rotateY(-90deg);
    -moz-transform-origin: 100% 50%;
  }
  .barleft {
    height: inherit;
    left: 0;
    -webkit-transform: rotateY(90deg);
    -webkit-transform-origin: 0% 50%;
    -moz-transform: rotateY(90deg);
    -moz-transform-origin: 0% 50%;
  }

</style>

在CSS代碼中,我們沒有定義圖表中條形圖的位置或顏色。這需要為每個元素單獨(dú)完成。但請注意,我們在可能的情況下使用了inherit屬性來簡化這一過程。

3、條形圖HTML標(biāo)記

在這里,您可以看到實踐中用于下面演示的代碼。圖表上有三個條形圖。每個酒吧都是一個div,有四個孩子div組成四邊。您可以擁有任意數(shù)量的條形圖并將它們放置在圖表上的任何位置。

<div id="stage">
	<div id="chart">

		<div class="bar" style="left: 80px; background: rgba(255,0,0,0.8); -webkit-transform: translateZ(80px); -moz-transform: translateZ(80px);">
			<div class="barfront" style="height: 80px;"></div>
			<div class="barback" style="height: 80px;"></div>
			<div class="barright" style="width: 80px;"></div>
			<div class="barleft" style="width: 80px;"></div>
			20
		</div>

		<div class="bar" style="left: 120px; background: rgba(0,127,255,0.8); -webkit-transform: translateZ(120px); -moz-transform: translateZ(120px);">
			<div class="barfront" style="height: 120px;"></div>
			<div class="barback" style="height: 120px;"></div>
			<div class="barright" style="width: 120px;"></div>
			<div class="barleft" style="width: 120px;"></div>
			30
		</div>

		<div class="bar" style="left: 160px; background: rgba(255,255,0,0.8); -webkit-transform: translateZ(40px); -moz-transform: translateZ(40px);">
			<div class="barfront" style="height: 40px;"></div>
			<div class="barback" style="height: 40px;"></div>
			<div class="barright" style="width: 40px;"></div>
			<div class="barleft" style="width: 40px;"></div>
			10
		</div>

	</div>
</div>

在上面的代碼中,您可以看到突出顯示設(shè)置圖表中條形圖的x位置的代碼以及每個條形圖的高度(需要為構(gòu)成條形圖的每個元素定義)。在那里我們應(yīng)用的顏色(紅色,藍(lán)色,黃色)略微透明。

4、最終結(jié)果

如果您使用的是WebKit瀏覽器(Safari,Chrome,iPhone,iPad),那么您應(yīng)該看到3D條形圖以及一些可用于修改某些值的滑塊。在Firefox中,條形圖有一些人工制品,滑塊呈現(xiàn)為普通輸入框,但仍然有效。

css創(chuàng)建3D立體條形圖的方法

說明

可以通過修改.bar盒子的數(shù)值來實現(xiàn)條形柱的高度變化,例:

<div class="bar" style="left: 120px; background: rgba(0,127,255,0.8); -webkit-transform: translateZ(180px); -moz-transform: translateZ(120px);">
	<div class="barfront" style="height: 180px;"></div>
	<div class="barback" style="height: 180px;"></div>
	<div class="barright" style="width: 180px;"></div>
	<div class="barleft" style="width: 180px;"></div>
	30
</div>

css創(chuàng)建3D立體條形圖的方法

修改#stage盒子與#chart盒子里的值,可以透過不同的角度觀看條形圖

#stage {
-webkit-perspective: 1200px;
-webkit-perspective-origin: 60% 0%;
-moz-perspective: 1200px;
-moz-perspective-origin: 60% 0%;
background: rgba(0, 255, 255, 0.2);
}

css創(chuàng)建3D立體條形圖的方法

#chart {
	-webkit-transform-origin: 50% 50%;
	-webkit-transform: rotateX(22deg);
	-webkit-transform-style: preserve-3d;
	-moz-transform-origin: 50% 50%;
	-moz-transform: rotateX(22deg);
	-moz-transform-style: preserve-3d;
}

css創(chuàng)建3D立體條形圖的方法

感謝各位的閱讀!關(guān)于css創(chuàng)建3D立體條形圖的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI