上篇文件介绍了如何实现九宫格图片展示:css布局入门级实战之九宫格网格布局.不过存在一个问题:图片之间没有间距,用户体验欠佳;基于以上问题,本文进行优化.
较之前实现样式做以下调整:四张图按照两行显示,每行显示三个.不足的显示空白.之前是两行两列显示.
对应九宫格样式布局,这里优化一下display布局方式,采用grid的方式进行处理.
简单交代一下使用到的属性:
grid-template-columns:指定显示每列显示的长度,可以使用repeat函数进行简化书写;
grid-template-rows:指定每行显示的长度.
grid-gap:行与列之间的距离
以下是简化的项目代码:
<template>
<view>
<view class="user_class">
<u-avatar :src="userImg" size="30"></u-avatar>
<text>{{userName}}</text>
</view>
<view class="content">
<text>{{contentText}}</text>
</view>
<view class="img_class" :style="'height:'+imgClassHeight+'px;'">
<view class="img_content" v-if="imgClassHeight != 5" :style="'height:'+imgClassHeight+'px;grid-template-columns:repeat(3,'+imgWidth+'px);grid-template-rows:auto;'">
<image v-for="(dynaicImg,index) in contentImgArray" :key="index" :src="dynaicImg" mode="scaleToFill" :style="'height:'+imgHeight+'px;'+'width:'+imgWidth+'px'"></image>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
// 图片区域大小,根据图片数量动态变化
imgClassHeight: 0,
// 图片区域是否换行
imgClassFlexWrap: '',
// 图片宽度
imgWidth: 400,
// 图片高度
imgHeight: 400,
// 屏幕宽度大小
screenWith: 0,
contentImg:'',
contentText:'',
userImg:'',
userName:'',
contentImgArray:[],
commentCount:0,
createTime:'',
zanCount:0,
oneCommentInfoList:[],
keyboardHeight:0,
commentFocus: false,
dynamicId:0,
dynamicUserId:0,
comment: '' // 文本框输入评论信息
};
},
async onLoad(event) {
await uni.request({
url: 'https://abc:8080/abc/findDynamicInfo', //仅为示例,并非真实接口地址。
data:{
dynamicId:1
},
success: (response) => {
this.contentImg=response.data.data.contentImg,
this.contentText=response.data.data.contentText,
this.userImg=response.data.data.userImg,
this.userName=response.data.data.userName,
this.contentImgArray=response.data.data.contentImgArray,
this.commentCount=response.data.data.commentCount,
this.createTime=response.data.data.createTime,
this.zanCount=response.data.data.zanCount,
this.dynamicUserId=response.data.data.userId,
this.handleImg(response.data.data.contentImgArray.length)
}
});
},
methods:{
handleImg(dynamicImgLength){
// 处理success中无法获取data中属性问题,success中this非vue实例
let that = this
// 获取屏幕宽度信息
uni.getSystemInfo({
success:function(res) {
console.log("屏幕宽度:"+res.windowWidth); // 单位:px
that.screenWith=res.windowWidth;
console.log("screenWith:"+that.screenWith);
}
})
// 模拟服务器获取的图片数量
const imgSize=dynamicImgLength;
// 宽高比:1:1.2
const WidthHeightRtio=1.2;
//设置图片区域大小
if(imgSize == 0){ // 无图片时图片区域大小,默认不显示
this.imgClassHeight=5;
}
if(imgSize == 1){ // 1张,一行展示
this.imgClassHeight=240;
// 宽高比:1:1.2
this.imgWidth=200;
this.imgHeight=240
}
if(imgSize >= 2 && imgSize <=3){ // 2-3张,一行展示
this.imgWidth=(this.screenWith-20)/imgSize;
// 按照页面实际显示保持宽高比
console.log("图片个数:"+imgSize+",每张图大小:"+(this.imgWidth));
this.imgHeight=this.imgWidth * WidthHeightRtio
console.log("imgHeight:"+this.imgHeight);
this.imgClassHeight=this.imgHeight+2; // 加间距
console.log("imgClassHeight:"+this.imgClassHeight);
}
if(imgSize >= 4 && imgSize <=6){ // 4-6张两行
// 左右padding为20rpx,所以屏幕宽度需要减去20px,1rpx=0.5px
this.imgWidth=(this.screenWith-20)/3; // 每张图片宽度
console.log("图片个数:"+imgSize+",每张图 大小:"+(this.imgWidth));
this.imgHeight=this.imgWidth*WidthHeightRtio;
console.log("imgHeight:"+this.imgHeight);
// this.imgClassFlexWrap='wrap';
this.imgClassHeight=this.imgHeight * 2+2; // 加间距
console.log("imgClassHeight:"+this.imgClassHeight);
}
if(imgSize >= 7 && imgSize <=9){ // 7-9张三行
this.imgWidth=(this.screenWith-20)/3; // 每张图片宽度
this.imgHeight=this.imgWidth*WidthHeightRtio;
this.imgClassFlexWrap='wrap';
this.imgClassHeight=this.imgHeight * 3;
}
}
}
}
</script>
<style lang="scss">
.user_class{
padding-top: 30rpx;
height: 60rpx;
width: 100%;
display: flex;
justify-content: flex-start;
align-items: center;
padding-left: 20rpx;
text{
padding-left: 20rpx;
}
}
.content{
height: 150rpx;
width: 100%;
display: flex;
justify-content: flex-start;
align-items: center;
padding-left: 20rpx;
}
.img_class{
width: 100%;
.img_content{
padding-left: 20rpx;
padding-right: 20rpx;
display: grid;
grid-gap: 2px;// 间距
}
}
</style>
最终实现效果截图:

如果出现列之间间隔过大,调节间距不生效的现象,可以从script中尝试设置相同的行宽与列宽,修改行宽以及列宽修改间隔显示,可以替代grid-gap:
grid-template-columns:repeat(3,83px);
grid-template-rows:repeat(3,83px);
简单记录一下希望对有同样需求的同学有所帮助!
我在MiniTest::Spec和Capybara中使用以下规范:find_field('Email').must_have_css('[autofocus]')检查名为“电子邮件”的字段是否具有autofocus属性。doc说如下:has_css?(path,options={})ChecksifagivenCSSselectorisonthepageorcurrentnode.据我了解,字段“Email”是一个节点,因此调用must_have_css绝对有效!我做错了什么? 最佳答案 通过JonasNicklas得到了答案:No
是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'
我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes
@作者:SYFStrive @博客首页:HomePage📜:微信小程序📌:个人社区(欢迎大佬们加入)👉:社区链接🔗📌:觉得文章不错可以点点关注👉:专栏连接🔗💃:感谢支持,学累了可以先看小段由小胖给大家带来的街舞👉微信小程序(🔥)目录自定义组件-behaviors 1、什么是behaviors 2、behaviors的工作方式 3、创建behavior 4、导入并使用behavior 5、behavior中所有可用的节点 6、同名字段的覆盖和组合规则总结最后自定义组件-behaviors 1、什么是behaviorsbehaviors是小程序中,用于实现
有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.
我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO
首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟
我正在尝试动态构建一个多维数组。我想要的基本上是这样的(为简单起见写出来):b=0test=[[]]test[b]这给了我错误:NoMethodError:undefinedmethod`test=[[],[],[]]而且它工作正常,但在我的实际使用中,我不会事先知道需要多少个数组。有一个更好的方法吗?谢谢 最佳答案 不需要像您正在使用的索引变量。只需将每个数组附加到您的test数组:irb>test=[]=>[]irb>test[["a","b","c"]]irb>test[["a","b","c"],["d","e","f"]]
如何只加载map边界内的标记gmaps4rails?当然,在平移和/或缩放后加载新的。与此直接相关的是,如何获取map的当前边界和缩放级别? 最佳答案 我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件监听器:在你看来(index.html.erb):{"zoom"=>15,"auto_adjust"=>false,"detect_location"=>true,"center_on_user"=>true}},false,true)%>在View的底部添加:functiongmaps4rail
如何在对象上调用方法名称的嵌套哈希?例如,给定以下哈希:hash={:a=>{:b=>{:c=>:d}}}我想创建一个方法,给定上面的散列,执行以下操作:object.send(:a).send(:b).send(:c).send(:d)我的想法是我需要从一个未知的关联中获取一个特定的属性(这个方法不知道,但程序员知道)。我希望能够指定一个方法链来以嵌套哈希的形式检索该属性。例如:hash={:manufacturer=>{:addresses=>{:first=>:postal_code}}}car.execute_method_hash(hash)=>90210