首先在模型中创建这个类
public class ImageModel
{
List<string> _images = new List<string>();
public ImageModel()
{
_images = new List<string>();
}
public List<string> Images
{
get { return _images; }
set { _images = value; }
}
}
然后在控制器中使用以下代码
var imageFiles = new ProjectName.Models.ImageModel();
imageFiles.Images.AddRange(System.IO.Directory.GetFiles(imagepath));
return View(imageFiles);
在视图中,您将其用作
@model ImageModel
<div id="container">
<div class="photobanner">
@for (int imgIndex = 0; imgIndex < Model.Images.Count; imgIndex++)
{
if (imgIndex == 0)
{
<img class="first" src = "@Model.Images[imgIndex]" alt="No Image" style="max-height: 110px; "/>
}
else
{
<img src = "@Model.Images[imgIndex]" alt="No Image" style="max-height: 110px; "/>
}
}
</div>
您必须将 css 应用为
/*photobanner*/
.photobanner {
height: 233px;
width: 3550px;
margin-bottom: 80px;
}
/*keyframe animations*/
.first {
-webkit-animation: bannermove 30s linear infinite;
-moz-animation: bannermove 30s linear infinite;
-ms-animation: bannermove 30s linear infinite;
-o-animation: bannermove 30s linear infinite;
animation: bannermove 30s linear infinite;
}
@keyframes "bannermove" {
0% {
margin-left: 0px;
}
100% {
margin-left: -2125px;
}
}
@-moz-keyframes bannermove {
0% {
margin-left: 0px;
}
100% {
margin-left: -2125px;
}
}
@-webkit-keyframes "bannermove" {
0% {
margin-left: 0px;
}
100% {
margin-left: -2125px;
}
}
@-ms-keyframes "bannermove" {
0% {
margin-left: 0px;
}
100% {
margin-left: -2125px;
}
}
@-o-keyframes "bannermove" {
0% {
margin-left: 0px;
}
100% {
margin-left: -2125px;
}
}
.photobanner {
height: 233px;
width: 3550px;
margin-bottom: 80px;
}
.photobanner img {
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease;
}
.photobanner img:hover {
-webkit-transform: scale(1.1);
-moz-transform: scale(1.1);
-o-transform: scale(1.1);
-ms-transform: scale(1.1);
transform: scale(1.1);
cursor: pointer;
-webkit-box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
-moz-box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
}
这段代码是我从我身边尝试过的工作代码。希望对您有所帮助。在图像路径中指定完整路径说文件所在的物理路径