Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
avascript實(shí)現(xiàn)左右點(diǎn)擊滑動(dòng)輪播效果,一般用在一些產(chǎn)品展示列表欄下,方便左右點(diǎn)擊查看更多展示產(chǎn)品圖片!
效果圖如下:
當(dāng)鼠標(biāo)移上內(nèi)容區(qū)域時(shí)候,左右箭頭會(huì)展示出來!點(diǎn)擊即可隨著點(diǎn)擊方向移到相應(yīng)的位置!
實(shí)現(xiàn)代碼如下:
html:
css樣式:
javascript:
角形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>三角形</title>
<style type="text/css">
body{
background-color: pink;
}
div{
width: 0px;
height: 0px;
border-top: 10px solid blue;
border-right: 10px solid transparent;
border-bottom: 10px solid transparent;
border-left: 10px solid transparent;
/* 唰(忽略不計(jì)) */
/* border-width: 10px;
border-style: solid;
border-color: green orange blue pink; */
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
箭頭
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#bigBox{
width: 0;
height: 0;
border-top: 200px solid black;
border-right: 200px solid transparent;
border-bottom: 200px solid transparent;
border-left: 200px solid transparent;
}
#smallBox{
width: 0;
height: 0;
border-top: 160px solid white;
border-right: 160px solid transparent;
border-bottom: 160px solid transparent;
border-left: 160px solid transparent;
position: absolute;
left: 40px;
top: 0;
}
</style>
</head>
<body>
<div id="bigBox"></div>
<div id="smallBox"></div>
</body>
</html>
三角形使用案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
list-style: none;
line-height: 40px;
cursor: pointer;
}
ul{
width: 200px;
margin: 0 auto;
border: 1px solid #000;
}
h3{
text-indent: 1em;
background-color: dodgerblue;
position: relative;
}
h3:before{
content: "";
width: 0;
height: 0;
border-top: 8px solid transparent;
border-right: 8px solid transparent;
border-bottom: 8px solid transparent;
border-left: 8px solid #000;
position: absolute;
left: 5px;
top: 12px;
}
h3.active:before{
border-top: 8px solid #000;
border-left: 8px solid transparent;
left: 0px;
top: 15px;
}
</style>
</head>
<body>
<ul>
<li>
<h3>我的好友</h3>
</li>
<li>
<h3 class="active">企業(yè)好友</h3>
</li>
<li>
<h3>黑名單</h3>
</li>
</ul>
</body>
</html>
制作箭頭
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#bigBox{
width: 0;
height: 0;
border-top: 200px solid black;
border-right: 200px solid transparent;
border-bottom: 200px solid transparent;
border-left: 200px solid transparent;
}
#smallBox{
width: 0;
height: 0;
border-top: 160px solid white;
border-right: 160px solid transparent;
border-bottom: 160px solid transparent;
border-left: 160px solid transparent;
position: absolute;
left: 40px;
top: 0;
}
</style>
</head>
<body>
<div id="bigBox"></div>
<div id="smallBox"></div>
</body>
</html>
實(shí)例: 實(shí)心箭頭上下左右
我們的開發(fā)工程中經(jīng)常會(huì)使用到各種圖,所謂的圖就是由節(jié)點(diǎn)和節(jié)點(diǎn)之間的連接所形成的系統(tǒng),數(shù)學(xué)上專門有一個(gè)分支叫圖論(Graph Theroy)。利用圖我們可以做很多工具,比如思維導(dǎo)圖,流程圖,狀態(tài)機(jī),組織架構(gòu)圖,等等。今天我要做的是用開源的HTML5工具來快速構(gòu)造一個(gè)做圖的工具。
工預(yù)善其事,必先利其器。第一件事是選擇一件合適的工具,開源時(shí)代,程序員還是很幸福的,選擇很多。
最終,我選擇了jsPlumb,因?yàn)樗耆_源,使用很簡單,用D3的話可能會(huì)多花很多功夫。joint.js也不錯(cuò)。大家可以根據(jù)自己的需要選擇。
下面我們一步一步的來使用jsPlumb來創(chuàng)建我們的流程圖工具。
第一步是等待DOM和jsPlumb初始化完畢,類似document.ready()和jquery.ready(), 要使用jsPlumb, 需要把代碼放在這個(gè)函數(shù)里:
jsPlumb.ready(function()?{ ????//?...?your?code?goes?here?... }
創(chuàng)建一個(gè)jsPlumb的實(shí)例,并初始化jsPlumb的配置參數(shù):
//Initialize?JsPlumb var?color?=?"#E8C870"; var?instance?=?jsPlumb.getInstance({ ????//?notice?the?'curviness'?argument?to?this?Bezier?curve.??the?curves?on?this?page?are?far?smoother ????//?than?the?curves?on?the?first?demo,?which?use?the?default?curviness?value.?????? ????Connector?:?[?"Bezier",?{?curviness:50?}?], ????DragOptions?:?{?cursor:?"pointer",?zIndex:2000?}, ????PaintStyle?:?{?strokeStyle:color,?lineWidth:2?}, ????EndpointStyle?:?{?radius:5,?fillStyle:color?}, ????HoverPaintStyle?:?{strokeStyle:"#7073EB"?}, ????EndpointHoverStyle?:?{fillStyle:"#7073EB"?}, ????Container:"container-id" ?});
這里給給出了一些配置包括,連接線(這里配置了一個(gè)貝塞爾曲線),線的風(fēng)格,連接點(diǎn)得風(fēng)格。Container需要配置一個(gè)對(duì)應(yīng)的DIV容器的id。(這里也可以使用setContainer的方法)
下面我們要?jiǎng)?chuàng)建一個(gè)節(jié)點(diǎn)(node),每一個(gè)節(jié)點(diǎn)可以用一個(gè)DIV來實(shí)現(xiàn)。我這里提供了一個(gè)函數(shù)來創(chuàng)建節(jié)點(diǎn)。
function?addNode(parentId,?nodeId,?nodeLable,?position)?{ ??var?panel?=?d3.select("#"?+?parentId); ??panel.append('div').style('width','120px').style('height','50px') ????.style('position','absolute') ????.style('top',position.y).style('left',position.x) ????.style('border','2px?#9DFFCA?solid').attr('align','center') ????.attr('id',nodeId).classed('node',true) ????.text(nodeLable); ??return?jsPlumb.getSelector('#'?+?nodeId)[0]; }
這里做的事情就是創(chuàng)建了一個(gè)DIV元素,并放在對(duì)應(yīng)的容器的制定位置上,注意為了支持拖拽的功能,必須使用position:absolute 。
我使用D3來操作DOM,大家可能會(huì)更習(xí)慣JQuery,這純屬個(gè)人喜好的問題。
最后返回創(chuàng)建節(jié)點(diǎn)的實(shí)例引用,這是的selector使用了jsPlumb.getSelector()方法,它和JQuery的selector是一樣的,這樣用的好處是你可以使用不同的DOM操作庫,例如Vanilla
下面我使用一個(gè)函數(shù)來創(chuàng)建端點(diǎn)/錨點(diǎn)(anchor),錨點(diǎn)就是節(jié)點(diǎn)上的連接點(diǎn),用于連接不同的節(jié)點(diǎn)。
function?addPorts(instance,?node,?ports,?type)?{ ??//Assume?horizental?layout ??var?number_of_ports?=?ports.length; ??var?i?=?0; ??var?height?=?$(node).height();??//Note,?jquery?does?not?include?border?for?height ??var?y_offset?=?1?/?(?number_of_ports?+?1); ??var?y?=?0; ??for?(?;?i?<?number_of_ports;?i++?)?{ ????var?anchor?=?[0,0,0,0]; ????var?paintStyle?=?{?radius:5,?fillStyle:'#FF8891'?}; ????var?isSource?=?false,?isTarget?=?false; ????if?(?type?===?'output'?)?{ ??????anchor[0]?=?1; ??????paintStyle.fillStyle?=?'#D4FFD6'; ??????isSource?=?true; ????}?else?{ ??????isTarget?=true; ????} ????anchor[1]?=?y?+?y_offset; ????y?=?anchor[1]; ????instance.addEndpoint(node,?{ ??????uuid:node.getAttribute("id")?+?"-"?+?ports[i], ??????paintStyle:?paintStyle, ??????anchor:anchor, ??????maxConnections:-1, ??????isSource:isSource, ??????isTarget:isTarget ????}); ??} }
instance是jsPlumb的實(shí)例
node是我們用addNode方法創(chuàng)建的Node實(shí)例
ports,是一個(gè)string的數(shù)組,指定端點(diǎn)的個(gè)數(shù)和名字
type,可能是output或者input,指定端點(diǎn)的種類,一個(gè)節(jié)點(diǎn)的輸出端口可以連接另一個(gè)節(jié)點(diǎn)的輸入端口。
這里anchor是一個(gè)四維數(shù)組,0維和1維分別是錨點(diǎn)在節(jié)點(diǎn)x軸和y軸的偏移百分比。我這里希望把端口畫在節(jié)點(diǎn)的左右兩側(cè),并按照端口的數(shù)量均勻分布。
最后使用instance.addEndpoint來創(chuàng)建端點(diǎn)。注意這里只要指定isSource和isTarget就可以用drag&drop的方式來連接端點(diǎn),非常方便。
下面一步我們提供一個(gè)函數(shù)來連接端點(diǎn):
function?connectPorts(instance,?node1,?port1,?node2?,?port2)?{ ??//?declare?some?common?values: ??var?color?=?"gray"; ??var?arrowCommon?=?{?foldback:0.8,?fillStyle:color,?width:5?}, ??//?use?three-arg?spec?to?create?two?different?arrows?with?the?common?values: ??overlays?=?[ ????[?"Arrow",?{?location:0.8?},?arrowCommon?], ????[?"Arrow",?{?location:0.2,?direction:-1?},?arrowCommon?] ??]; ??var?uuid_source?=?node1.getAttribute("id")?+?"-"?+?port1; ??var?uuid_target?=?node2.getAttribute("id")?+?"-"?+?port2; ??instance.connect({uuids:[uuid_source,?uuid_target]}); }
node1和node2是源節(jié)點(diǎn)和目標(biāo)節(jié)點(diǎn)的引用,port1和port2是源端口和目標(biāo)端口的名字。
使用instance.connect方法來創(chuàng)建連接。 overlays用來添加連接線的箭頭效果或者其他風(fēng)格,我這里沒有使用,因?yàn)橛X得都不是很好看。大家如果要用,只要把overlays加入到instance.connect的方法參數(shù)就可以了。
調(diào)用以上方法來創(chuàng)建節(jié)點(diǎn),端點(diǎn)和連接線。
var?node1?=?addNode('container-id','node1',?'node1',?{x:'80px',y:'20px'}); var?node2?=?addNode('container-id','node2',?'node2',?{x:'280px',y:'20px'}); addPorts(instance,?node1,?['out1','out2'],'output'); addPorts(instance,?node2,?['in','in1','in2'],'input'); connectPorts(instance,?node1,?'out2',?node2,?'in');
這里我們創(chuàng)建了兩個(gè)節(jié)點(diǎn),第一個(gè)節(jié)點(diǎn)有兩個(gè)輸出端口,第二個(gè)節(jié)點(diǎn)有三個(gè)輸入端口,然后把第一個(gè)節(jié)點(diǎn)的out2端口連接到第二個(gè)端點(diǎn)的in端口。效果如下:
最后我們給節(jié)點(diǎn)增加drag&drop的功能,這樣我們就可以拖動(dòng)這些節(jié)點(diǎn)來改變圖的布局了。
instance.draggable($('.node'));
這里似乎依賴于JQuery-UI,我還不是很清楚。
我們已經(jīng)初步具有了創(chuàng)建圖的功能,可是節(jié)點(diǎn)的創(chuàng)建必須通過程序,我們希望用交互的方式來創(chuàng)建節(jié)點(diǎn)。
通常我們希望有一個(gè)tree view的控件,讓后通過拖拽來創(chuàng)建對(duì)應(yīng)類型的節(jié)點(diǎn)。這里我使用了這個(gè)開源的tree view,基于bootstrap https://github.com/jonmiles/bootstrap-treeview
我們先創(chuàng)建一個(gè)tree view:
function?getTreeData()?{ ??var?tree?=?[ ????{ ??????text:?"Nodes", ??????nodes:?[ ????????{ ??????????text:?"Node1", ????????}, ????????{ ??????????text:?"Node2" ????????} ??????] ????} ??];? ??return?tree; } //Initialize?Control?Tree?View $('#control-panel').treeview({data:?getTreeData()});
樹上有兩個(gè)節(jié)點(diǎn):
然后我實(shí)現(xiàn)從樹上拖拽對(duì)應(yīng)的節(jié)點(diǎn),到流程圖上的邏輯。
//Handle?drag?and?drop $('.list-group-item').attr('draggable','true').on('dragstart',?function(ev){ ??//ev.dataTransfer.setData("text",?ev.target.id); ??ev.originalEvent.dataTransfer.setData('text',ev.target.textContent); ??console.log('drag?start'); }); $('#container-id').on('drop',?function(ev){ ??//avoid?event?conlict?for?jsPlumb ??if?(ev.target.className.indexOf('_jsPlumb')?>=?0?)?{ ????return; ??} ??ev.preventDefault(); ??var?mx?=?''?+?ev.originalEvent.offsetX?+?'px'; ??var?my?=?''?+?ev.originalEvent.offsetY?+?'px'; ??console.log('on?drop?:?'?+?ev.originalEvent.dataTransfer.getData('text')); ??var?uid?=?new?Date().getTime(); ??var?node?=?addNode('flow-panel','node'?+?uid,?'node',?{x:mx,y:my}); ??addPorts(instance,?node,?['out'],'output'); ??addPorts(instance,?node,?['in1','in2'],'input'); ??instance.draggable($(node)); }).on('dragover',?function(ev){ ??ev.preventDefault(); ??console.log('on?drag?over'); });
這里要注意的是要避免和jsPlumb拖拽端點(diǎn)的邏輯沖突,當(dāng)檢測(cè)到target是jsPlumb對(duì)象是需要直接從drop方法中退出以執(zhí)行對(duì)應(yīng)的jsPlumb的drop邏輯。
好了,一個(gè)繪制流程圖的軟件工具初步完工。
我把代碼放在oschina的代碼托管服務(wù)上了, 大家有興趣可以去試試。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。