【问题标题】:How to change the shape of JQuery/CSS elements?如何改变 JQuery/CSS 元素的形状?
【发布时间】:2019-11-28 18:32:08
【问题描述】:

我正在尝试使用 JQuery 流程图库。我找到了一个带有以下 css 代码的示例(此处:https://github.com/nephilimboy/Angular_FlowChartJS):

body {
    font-family: 'Helvetica Neue', Helvetica, Arial, serif;
    font-size: 15px;
    font-weight: 400;
    line-height: 1.5;
    color: #666;
  }

  #chart_container {
    width: 100%;
    height: 500px;
    overflow: hidden;
    background: repeating-linear-gradient(
      45deg,
      #eee,
      #eee 10px,
      #e5e5e5 10px,
      #e5e5e5 20px
    );
    border: 1px solid black;
  }

  #exampleDiv {
    width: 2000px;
    height: 2000px;
    background: white;
  }

  .flowchart-example-container {
    height: 200px;
    border: 1px solid #BBB;
    margin-bottom: 10px;
  }

  /* #exampleDiv {
    width: 100%;
    height: 2000px;
    background: white;
  } */

  .draggable_operators_label {
    margin-bottom: 5px;
  }

  .draggable_operators_divs {
    margin-bottom: 20px;
  }

  .draggable_operator {
    display: inline-block;
    padding: 2px 5px 2px 5px;
    border: 1px solid #ccc;
    cursor: grab;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }



.flowchart-container {
    position: relative;
    overflow: hidden;
}

.flowchart-links-layer, .flowchart-operators-layer, .flowchart-temporary-link-layer {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
}

.flowchart-operators-layer, .flowchart-temporary-link-layer {
    pointer-events: none;
}

.flowchart-temporary-link-layer {
    display: none;
}



.flowchart-link, .flowchart-operator {
    cursor: default;
}


.flowchart-operator-connector {
    position: relative;
    padding-top: 5px;
    padding-bottom: 5px;
}

.flowchart-operator-connector-label {
    font-size: small;
}

.flowchart-operator-inputs .flowchart-operator-connector-label {
    margin-left: 14px;
}

.flowchart-operator-outputs .flowchart-operator-connector-label {
    text-align: right;
    margin-right: 5px;
}

.flowchart-operator-connector-arrow {
    width: 0px;
    height: 0px;
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-left: 10px solid rgb(204, 204, 204);
    position: absolute;
    top: 0px;
}

.flowchart-operator-connector-small-arrow {
    width: 0px;
    height: 0px;
    border-top: 5px solid transparent;
    border-bottom: 5px solid transparent;
    border-left: 5px solid transparent; /*rgb(100, 100, 100);*/
    position: absolute;
    top: 5px;
    pointer-events: none;
}

.flowchart-operator-connector:hover .flowchart-operator-connector-arrow {
    border-left: 10px solid rgb(105, 255, 5);
}

.flowchart-operator-inputs .flowchart-operator-connector-arrow {
    left: -1px;
}

.flowchart-operator-outputs .flowchart-operator-connector-arrow {
    right: -10px;
}

.flowchart-operator-inputs .flowchart-operator-connector-small-arrow {
    left: -1px;
}

.flowchart-operator-outputs .flowchart-operator-connector-small-arrow {
    right: -7px;
}

.unselectable {
   -moz-user-select: none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   /*
     Introduced in IE 10.
     See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
   */
   -ms-user-select: none;
   user-select: none;
}


/* Default Operator */

.flowchart-operator {
    position: absolute;
    width: 140px;
    border: 1px solid #CCCCCC;
    background: #FAFAFA;
    pointer-events: initial;
}

.flowchart-operator:hover {
    border-color: rgb(255, 238, 0) !important;
}

.flowchart-operator.selected {
    /* border-color: #555; */
    border: 5px solid black;
}



.flowchart-operator .flowchart-operator-title {
    width: 100%;
    padding: 5px;
    font-weight: bold;
    box-sizing: border-box;
    border-bottom: 1px solid #DDDDDD;
    /* background: #F0F0F0; */
    height: auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: move;


    /* background-color: #2aa58f;
    color: white; */
}

.flowchart-operator .flowchart-operator-inputs-outputs {
    display: table;
    width: 100%;
    margin-top: 5px;
    margin-bottom: 5px;
}

.flowchart-operator .flowchart-operator-inputs, .flowchart-default-operator .flowchart-operator-outputs {
    display: table-cell;
    width: 50%;
}


/* .flowchart-link > path{
    stroke: #2aa58f;
} */




.myTest{
    background-color: #2a7ea5;
    color: white;
}

.myTest2{
    background-color: #4c0f8d;;
    color: white;
}

.myTest3{
    background-color: #2e4692;;
    color: white;
}

.myTest4{
    background-color: #8d0f15;;
    color: white;
}

它会创建这样的东西:

但我喜欢创建其他形状,例如圆形,而不仅仅是矩形。我该怎么做?

编辑:这是component.html 文件的内容:

<div id="chart_container" #chart_container>
    <!-- <pan-zoom [config]="panzoomConfig"> -->
    <div class="flowchart-example-container" id="exampleDiv" #exampleDiv></div>
    <!-- </pan-zoom> -->
  </div>


<input type="button" value="add input" (click)="addNewOperator3()">
<input type="button" value="add operator" (click)="addNewOperator()">
<input type="button" value="add operator2" (click)="addNewOperator2()">
<input type="button" value="add output" (click)="addNewOperator4()">

<input type="button" value="delete" (click)="deleteOperationOrLink()">
<input type="button" value="load" (click)="load()">
<input type="button" value="get" (click)="get()">
<br />
<textarea nbInput fullWidth shape="round" name="diagModel" [(ngModel)]="diagModel" placeholder="model"
  style="height: 300px; width: 500px"></textarea>

component.ts 文件的内容:

import { Component, ViewChild, ElementRef, OnInit, AfterViewInit, AfterContentInit, AfterViewChecked, AfterContentChecked } from '@angular/core';
import {
  Compiler,
  ComponentFactory,
  ComponentFactoryResolver,
  ComponentRef,
  Input,
  ModuleWithComponentFactories,
  NgModule,
  ViewContainerRef
} from '@angular/core';

import { flyInOut, expand } from '../animations/app.animation';


declare var $: any;


@Component({
  selector: 'flow-home',
  templateUrl: './flow.component.html',
  styleUrls: ['./flow.component.scss'],
  host: {
    '[@flyInOut]': 'true',
    'style': 'display: block;'
  },
  animations: [
    flyInOut(),
    expand()
  ]
})

export class FlowComponent implements AfterViewInit {

  public diagModel: any;

  private cx: number;
  private cy: number;



  @ViewChild('exampleDiv',{static:true}) exampleDiv: ElementRef;



  constructor() {
  }



  ngAfterViewInit() {


    var container = $('#chart_container');
    this.cx = $('#exampleDiv').width() / 2;
    this.cy = $('#exampleDiv').height() / 2;
    $('#exampleDiv').panzoom({
    });
    $('#exampleDiv').panzoom('pan', -this.cx + container.width() / 2, -this.cy + container.height() / 2);

    var possibleZooms = [0.5, 0.75, 1, 2, 3];
    var currentZoom = 2;
    container.on('mousewheel.focal', function (e) {
      e.preventDefault();
      var delta = (e.delta || e.originalEvent.wheelDelta) || e.originalEvent.detail;
      var zoomOut: any = delta ? delta < 0 : e.originalEvent.deltaY > 0;
      currentZoom = Math.max(0, Math.min(possibleZooms.length - 1, (currentZoom + (zoomOut * 2 - 1))));
      $('#exampleDiv').flowchart('setPositionRatio', possibleZooms[currentZoom]);
      $('#exampleDiv').panzoom('zoom', possibleZooms[currentZoom], {
        animate: false,
        focal: e
      });

    });


    setTimeout(() => {
      $(this.exampleDiv.nativeElement).flowchart({
        data: '',
        multipleLinksOnOutput: true,
      });

    }, 1000);

  }


  getOperatorData($element) {
    var nbInputs = parseInt($element.data('nb-inputs'));
    var nbOutputs = parseInt($element.data('nb-outputs'));
    var data = {
      properties: {
        title: $element.text(),
        inputs: {},
        outputs: {}
      }
    };

    var i = 0;
    for (i = 0; i < nbInputs; i++) {
      data.properties.inputs['input_' + i] = {
        label: 'Input ' + (i + 1)
      };
    }
    for (i = 0; i < nbOutputs; i++) {
      data.properties.outputs['output_' + i] = {
        label: 'Output ' + (i + 1)
      };
    }

    return data;
  }



  operatorI = 0;
  operatorII = 0;


  addNewOperator() {

    var operatorId = 'created_operator_' + this.operatorI;
    var operatorData = {
      top: this.cx,
      left: this.cy,
      properties: {
        title: 'Operator ' + (this.operatorI + 1),
        class: 'myTest',
        inputs: {
          input_1: {
            label: 'Input 1',
          },
          input_2: {
            label: 'Input 2',
          },
          input_3: {
            label: 'Input 3',
          },
        },
        outputs: {
          output_1: {
            label: 'Output 1',
          },
          output_2: {
            label: 'Output 2',
          },

        }
      }
    }

    this.operatorI++;
    $(this.exampleDiv.nativeElement).flowchart('createOperator', operatorId, operatorData);
  }

  addNewOperator2() {

    var operatorId = 'created_operator_' + this.operatorI;
    var operatorData = {
      top: this.cx,
      left: this.cy,
      properties: {
        title: 'Operator ' + (this.operatorI + 1),
        class: 'myTest2',
        inputs: {
          input_1: {
            label: 'Input 1',
          },
          input_2: {
            label: 'Input 2',
          },
          input_3: {
            label: 'Input 3',
          },
        },
        outputs: {
          output_1: {
            label: 'Output 1',
          },
          output_2: {
            label: 'Output 2',
          },
        }
      }
    }

    this.operatorI++;
    $(this.exampleDiv.nativeElement).flowchart('createOperator', operatorId, operatorData);
  }

  addNewOperator3() {

    var operatorId = 'created_operator_' + this.operatorI;
    var operatorData = {
      top: this.cx,
      left: this.cy,
      properties: {
        title: 'Input ' ,
        class: 'myTest3',
        inputs: {
          input_1: {
            label: 'Input 1',
          },
          input_2: {
            label: 'Input 2',
          },
          input_3: {
            label: 'Input 3',
          },
        },
        outputs: {
          output_1: {
            label: 'Output 1',
          },
          output_2: {
            label: 'Output 2',
          },

        }
      }
    }

    this.operatorI++;
    $(this.exampleDiv.nativeElement).flowchart('createOperator', operatorId, operatorData);
  }

  addNewOperator4() {

    var operatorId = 'created_operator_' + this.operatorI;
    var operatorData = {
      top: this.cx,
      left: this.cy,
      properties: {
        title: 'Output',
        class: 'myTest4',
        inputs: {
          input_1: {
            label: 'Input 1',
          },
          input_2: {
            label: 'Input 2',
          },
          input_3: {
            label: 'Input 3',
          },
        },
        outputs: {
          output_1: {
            label: 'Output 1',
          },
          output_2: {
            label: 'Output 2',
          },

        }
      }
    }

    this.operatorI++;
    $(this.exampleDiv.nativeElement).flowchart('createOperator', operatorId, operatorData);
  }


  deleteOperationOrLink() {
    $(this.exampleDiv.nativeElement).flowchart('deleteSelected');
  }

  load() {
    $(this.exampleDiv.nativeElement).flowchart('deleteSelected');
    var data = JSON.parse(this.diagModel);
    $(this.exampleDiv.nativeElement).flowchart('setData', data);
  }


  get() {
    $(this.exampleDiv.nativeElement).flowchart('deleteSelected');
    var data = $(this.exampleDiv.nativeElement).flowchart('getData');
    this.diagModel = JSON.stringify(data, null, 2);
  }

}

【问题讨论】:

    标签: jquery css flowchart


    【解决方案1】:

    对于圆,如果你不介意使用 css,那么只需使用 css 修改一个 div 或其他使用边框半径的元素。

    CSS:

    .circle {
     height: 50px;
     width: 50px;
     border-radius: 50%;
     background-color: black;
     }
    

    对于三角形,只需使用边框操作,使 div 的高度和宽度为空,并使边框成为显示因素。然后让它的一部分透明。

    CSS:

    .triangle {
      width: 0; 
      height: 0; 
      border-top: 50px solid transparent;
      border-bottom: 50px solid transparent; 
      border-right: 50px solid red; 
      }
    

    对于更高级的形状,您可能需要在元素的 css 上使用 before 和 after 子句。下面的例子。

    CSS:

    .hexagon {
     position: relative;
     width: 300px; 
     height: 173.21px;
     background-color: #64C7CC;
     margin: 86.60px 0;
     }
    
     .hexagon:before,
     .hexagon:after {
      content: "";
      position: absolute;
      width: 0;
      border-left: 150px solid transparent;
      border-right: 150px solid transparent;
      }
    
      .hexagon:before {
       bottom: 100%;
       border-bottom: 86.60px solid #64C7CC;
       }
    
      .hexagon:after {
       top: 100%;
       width: 0;
       border-top: 86.60px solid #64C7CC;
       }
    

    对于这些更高级的形状,有一些网站可以为您生成 css。用于生成上述六边形 css 的是 csshexagon.com。

    【讨论】:

    • 非常感谢 Tyler 的帮助。但是因为我是一个新的学习者,所以我有点困惑。我也添加了相关的html文件,请问我应该在哪里添加你放在这里的类?
    • 它们只是 div,所以对于圆圈,只需放置
      和三角形
      和六边形,
    • 那么上面代码中默认的矩形形状的名称是什么?
    • 如果我知道默认形状的名称,我想我可以用圆圈名称替换它。
    • 我也添加了component.ts文件。
    猜你喜欢
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多