php图片自适应手机屏幕,用JS让图片自适应手机屏幕

单纯的设置图片的样式 max-width: 100% 的情况下,如果在编辑器中设置过图片的高度会出现图片变形的情况

通过js可以解决因为设置图片height导致的图片变形问题

代码如下:

var image = $('.content img');

image.each(function () {

var $this = $(this);

var max_width = $this.parents('.content').css('width');

var width = $this.css('width').replace('px', '');

var height = $this.css('height').replace('px', '');

if(height == 0) {

return;

}

max_width = max_width.replace('px', '');

max_width = parseFloat(max_width);

width = parseFloat(width);

height = parseFloat(height);

if (width > max_width) {

height = height * (max_width / width);

width = max_width;

}

$this.attr('width', width);

$this.css('width', width + "px");

$this.attr('height', height);

$this.css('height', height + "px");

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个搜索页面的示例代码,包含了按块展示搜索结果的功能,并且使用了响应式设计,可以适应不同大小的屏幕: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>搜索页面</title> <style> /* 设置全局样式 */ body { margin: 0; padding: 0; font-family: Arial, sans-serif; } /* 设置搜索框样式 */ #search-box { display: flex; align-items: center; justify-content: center; height: 100px; background-color: #f5f5f5; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } #search-input { width: 80%; max-width: 600px; height: 40px; padding: 10px; border: none; border-radius: 20px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); font-size: 18px; outline: none; } #search-button { margin-left: 20px; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 20px; font-size: 18px; cursor: pointer; } /* 设置搜索结果样式 */ #search-results { display: flex; flex-wrap: wrap; align-items: stretch; justify-content: center; padding: 20px; } .search-result { width: 300px; margin: 10px; background-color: #fff; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); border-radius: 5px; overflow: hidden; } .search-result img { width: 100%; height: 200px; object-fit: cover; } .search-result h3 { margin: 10px; font-size: 20px; font-weight: bold; } .search-result p { margin: 10px; font-size: 16px; color: #666; line-height: 1.5; } /* 响应式设计 */ @media (max-width: 768px) { #search-input { width: 60%; } .search-result { width: 100%; } } </style> </head> <body> <!-- 搜索框 --> <div id="search-box"> <input type="text" id="search-input" placeholder="请输入搜索内容"> <button id="search-button">搜索</button> </div> <!-- 搜索结果 --> <div id="search-results"> <!-- 这里用 JavaScript 动态生成搜索结果 --> </div> <!-- 引入 jQuery 库 --> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> // 监听搜索按钮点击事件 $('#search-button').click(function() { // 获取搜索内容 var keyword = $('#search-input').val(); // 向服务器发送搜索请求,并获取搜索结果 $.get('search.php', {keyword: keyword}, function(data) { // 解析搜索结果,并动态生成搜索结果的 HTML 代码 var resultsHtml = ''; for (var i = 0; i < data.length; i++) { resultsHtml += '<div class="search-result">'; resultsHtml += '<img src="' + data[i].imageUrl + '">'; resultsHtml += '<h3>' + data[i].title + '</h3>'; resultsHtml += '<p>' + data[i].description + '</p>'; resultsHtml += '</div>'; } // 将搜索结果添加到页面上 $('#search-results').html(resultsHtml); }); }); </script> </body> </html> ``` 请注意,这里的搜索结果是通过 JavaScript 动态生成的,需要与服务器端的代码配合实现。另外,为了方便演示,这里使用了 jQuery 库,但实际开发中应该根据需要选择合适的工具和框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值