div 八个方向拉伸缩放

<!DOCTYPE html>
<html>
<head>
    <title></title>
<style>
 #cut{
    opacity:0.6;
height:150px;
width:150px;
position:absolute;
top:150px;
left:150px;
cursor:pointer;
    border:1px dotted red;

 }
 .box{
height:48.5%;
width:48.5%;
float:left;
background-color:#F0F0F0;
opacity:0.3;
 }
 .box-resize{
    border:1px solid black;
width:4px;
height:4px;
position:absolute;
 }  
 .box-top-left{
   top:-3px;
   left:-3px;
cursor:nw-resize;
 }
 .box-top-right{
    top:-3px;
right:-3px;
cursor:ne-resize;
 }
 .box-left-center{
   top:50%;
left:-3px;
cursor:w-resize;
 }
 .box-right-center{
    top:50%;
right:-3px;
cursor:e-resize;
 }
 .box-bottom-left{
   left:-3px;
bottom:-3px;
cursor:sw-resize;
 }
 .box-bottom-right{    
right:-3px;
bottom:-3px;
cursor:se-resize;
 }
 .box-top-center{
     left:50%;
 top:-3px;
 cursor:n-resize;
 }
 .box-bottom-center{
     left:50%;
 bottom:-3px;
 cursor:s-resize;
 }
</style>
</head>
<body>
<div id="cut">
   <div style="height:50%;width:100%;border-bottom:1px dotted red;position:absolute;top:2px;left:0;"></div>
   <div style="height:100%;width:50%;border-right:1px dotted red;position:absolute;z-index:10;top:0;left:2px"></div>
   <div class="box-resize box-top-left"></div>
   <div class="box-resize box-top-right"></div>
   <div class="box-resize box-left-center"></div>
        <div class="box-resize box-right-center"></div>
   <div class="box-resize box-bottom-left"></div>
        <div class="box-resize box-bottom-right"></div>
   <div class="box-resize box-top-center"></div>
   <div class="box-resize box-bottom-center"></div>
 
</div>
  <script> 
  window.οnlοad=function(){
      var resize=document.getElementsByClassName("box-resize");
      var cut=document.getElementById("cut");
 var cutWidth=0;
 var cutHeight=0;
 var startX=0;
      var startY=0;
 var top=0;
 var left=0;
 var dir="";
  for(var i=0;i<resize.length;i++){
   resize[i].οnmοusedοwn=function(e){
        startX=e.clientX;
startY=e.clientY;
    cutWidth=cut.offsetWidth;
                cutHeight=cut.offsetHeight;
top=cut.offsetTop;
left=cut.offsetLeft;
var className=this.className;
if(className.indexOf("box-right-center")>-1){
  dir="E";
}
else if(className.indexOf("box-top-left")>-1){
  dir="NW";
}
else if(className.indexOf("box-top-right")>-1){
  dir="NE";
}
 else if(className.indexOf("box-left-center")>-1){
  dir="W";
}
 else if(className.indexOf("box-bottom-left")>-1){
  dir="SW";
}
  else if(className.indexOf("box-bottom-right")>-1){
  dir="SE";
}
 else if(className.indexOf("box-bottom-center")>-1){
  dir="S";
}
     else if(className.indexOf("box-top-center")>-1){
  dir="N";
}

     document.addEventListener('mousemove',test);

                               e.preventDefault();

}
     }

  
         document.οnmοuseup=function(e){
     dir="";

    document.removeEventListener('mousemove',test);

                            e.preventDefault();

                }

function test(e){
  var width=e.clientX-startX;
var height=e.clientY-startY;
if(dir=="E"){
cut.style.width=cutWidth+width+"px";
}
else if(dir=="S"){
  cut.style.height=cutHeight+height+"px";
}
else if(dir=="N"){ 
   if(height<cutHeight)
{
     cut.style.height=cutHeight-height+"px";
     cut.style.top=top+height+"px";
  }
}
else if(dir=="W"){
   if(width<cutWidth){
  cut.style.width=cutWidth-width+"px";
  cut.style.left=left+width+"px";
  }
}
else if(dir=="NW"){
  if(width<cutWidth&&height<cutHeight){
     cut.style.width=cutWidth-width+"px";
 cut.style.height=cutHeight-height+"px";
 cut.style.top=top+height+"px";
 cut.style.left=left+width+"px";
  }
}
else if(dir=="NE"){  
if(height<cutHeight){
cut.style.width=cutWidth+width+"px";
cut.style.height=cutHeight-height+"px";
cut.style.top=top+height+"px";
}
}
else if(dir=="SW"){    
if(width<cutWidth){
cut.style.width=cutWidth-width+"px";
cut.style.height=cutHeight+height+"px";
cut.style.left=left+width+"px";
}
}
else if(dir=="SE"){
if(width<cutWidth){
cut.style.width=cutWidth+width+"px";
cut.style.height=cutHeight+height+"px";
}
}
}
}

  </script>
</body>
</html>

### 回答1: 好的,下面是一个示例代码: ```css img { width: 100%; height: auto; } ``` 这段代码可以使图片的宽度自动撑满容器,而高度自动等比缩放。 如果你想使图片的长边(无论是宽还是高)撑满容器,可以使用以下代码: ```css img { width: 100%; height: 100%; object-fit: cover; } ``` 这样图片就会被拉伸,使其长边撑满容器,短边会被裁剪。 希望这些代码能帮到你! ### 回答2: 要用CSS写一个拉伸图片效果,可以使用CSS的background-size属性来实现。 首先,在HTML中创建一个容器元素,可以是一个div或者其他块级元素。给该元素设置一个固定的宽度和高度,例如: <div class="image-container"></div> 然后,在CSS中,给该容器元素设置背景图片,并设置background-size属性为"100% 100%",这样图片就会水平和垂直方向都被拉伸以适应容器的大小。例如: .image-container { width: 300px; height: 200px; background-image: url("图片链接"); background-size: 100% 100%; } 其中,"图片链接"需要替换成你自己要使用的图片的链接。 最后,你可以根据需要继续添加其他样式,例如设置背景的重复方式、位置等。 通过以上步骤,你就可以使用CSS实现一个拉伸图片的效果了。 ### 回答3: 要使用CSS实现拉伸图片,可以使用`background-size`属性来控制背景图片的尺寸。以下是一个示例代码: ```html <div class="container"></div> ``` ```css .container { width: 300px; /* 设定容器的宽度 */ height: 200px; /* 设定容器的高度 */ background-image: url("your-image.jpg"); /* 设置背景图片的路径 */ background-repeat: no-repeat; /* 禁止图片重复显示 */ background-size: 100% 100%; /* 将背景图片拉伸至容器的尺寸 */ } ``` 在上述代码中,我们创建了一个具有指定宽度和高度的容器,并设置了背景图片的路径。通过将`background-size`属性设置为`100% 100%`,背景图片将被拉伸以填充整个容器的尺寸。此外,通过设置`background-repeat`属性为`no-repeat`,可以禁止图片在容器内重复显示。 记得将`your-image.jpg`替换为你自己的图片路径。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值