<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9HZTNaYy5wbmcifQ==” alt =“ triangle”>
我正在使用一个使用三角形覆盖其他容器/ div的应用程序。以前用CSS解决过吗:
.triangle:after {
content: "";
display: block;
position: absolute;
top: 15px;
left: -15px;
width: 0;
border-width: 0px 0px 15px 15px;
border-style: solid;
}
但是在React中这不再起作用。什么是去这里的最佳解决方案?
做到这一点的最佳方法是创建一个<Image>
组件并对其进行绝对定位,类似于纯CSS三角形的方式。如果三角形具有纯色,而不是渐变或其他图案,则可以使用tintColor
样式属性设置其颜色。
示例:
<Image
source={require('image!triangle')}
style={{tintColor: '#008080'}}
/>
仍然可以使用CSS技巧在React Native中绘制三角形。我写了一个类来封装它:https://github.com/Jpoliachik/react-native-triangle
[如果您想自己编写,我使用此工具:http://apps.eky.hk/css-triangle-generator/生成所需的三角形并将样式修改为React Native语法。
例如,在CSS中朝上的等腰三角形90x90读为:
width: 0;
height: 0;
border-style: solid;
border-width: 0 45px 90px 45px;
border-color: transparent transparent #007bff transparent;
但是在React Native中,样式将是:
triangle: {
width: 0,
height: 0,
backgroundColor: 'transparent',
borderStyle: 'solid',
borderTopWidth: 0,
borderRightWidth: 45,
borderBottomWidth: 90,
borderLeftWidth: 45,
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'red',
borderLeftColor: 'transparent',
},
render() {
return (
<View style={[styles.triangle,styles.arrowUp]}/>
);
}
和样式
const styles = {
triangle: {
width: 0,
height: 0,
backgroundColor: 'transparent',
borderStyle: 'solid',
},
arrowUp: {
borderTopWidth: 0,
borderRightWidth: 30,
borderBottomWidth: 30,
borderLeftWidth: 30,
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: "tomato",
borderLeftColor: 'transparent',
},
arrowRight: {
borderTopWidth: 30,
borderRightWidth: 0,
borderBottomWidth: 30,
borderLeftWidth: "tomato",
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: "tomato",
},
arrowDown: {
borderTopWidth: 30,
borderRightWidth: 30,
borderBottomWidth: 0,
borderLeftWidth: 30,
borderTopColor: "tomato",
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowLeft: {
borderTopWidth: 30,
borderRightWidth: "tomato",
borderBottomWidth: 30,
borderLeftWidth: 0,
borderTopColor: 'transparent',
borderRightColor: "tomato",
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowUpLeft: {
borderTopWidth: 30,
borderRightWidth: "tomato",
borderBottomWidth: 0,
borderLeftWidth: 0,
borderTopColor: "tomato",
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowUpRight: {
borderTopWidth: 0,
borderRightWidth: "tomato",
borderBottomWidth: 30,
borderLeftWidth: 0,
borderTopColor: 'transparent',
borderRightColor: "tomato",
borderBottomColor: 'transparent',
borderLeftColor: 'transparent',
},
arrowDownLeft: {
borderTopWidth: 30,
borderRightWidth: 0,
borderBottomWidth: 0,
borderLeftWidth: "tomato",
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: 'transparent',
borderLeftColor: "tomato",
},
arrowDownRight: {
borderTopWidth: 0,
borderRightWidth: 0,
borderBottomWidth: 30,
borderLeftWidth: "tomato",
borderTopColor: 'transparent',
borderRightColor: 'transparent',
borderBottomColor: "tomato",
borderLeftColor: 'transparent',
},
}
这是关于反应本机中建筑物形状和形式的很好的图图如果您想做更多的事情,这可能会激发一些基础知识。这是一个好的开始。当时对我有很大帮助:https://codedaily.io/tutorials/22/The-Shapes-of-React-Native