根据属性中指定的图像大小动态更改图像
Change image dynamically on the basis of its size specified in attribute
我正在构建一个CMS系统,用户可以在其中管理和上传图像以显示在博客上。
为了提高效率,我创建了三个不同大小的图像版本。但最终用户对此一无所知
我想要的是,当wyswyg编辑器用户更改其尺寸时,图像URL应该相应地更改,以适合"图标"、"拇指"answers"大"类型的图像。我可以通过解析服务器端的内容来做到这一点,但在客户端没有任何标准的方法吗?
假设您的图像有这样的url:http://example.com/image.jpg
你可以做一些类似的事情
$(document).ready(function(){
$('img').each(function(){
var src = $(this).attr('src');
//the extension of the image (e.g. png, gif, jpeg)
var extension = src.substr( (src.lastIndexOf('.') +1) );
//the path to the image, without the extension (and without the . )
var path = href.substr(0, href.length - extension.length - 1);
//we will store our new path here
var newSrc = '';
//get the correct path, depending on the size of the image
if($(this).width() < 150){
newSrc = path + '-icon.' + extension;
}
if($(this).width() < 350){
newSrc = path + '-thumb.' + extension;
}
if($(this).width() > 350){
newSrc = path + '-full.' + extension;
}
//give our image the new image path, to either an icon, thumb or full image
$(this).attr('src', newSrc);
}
});
相关文章:
- 使用JavaScript根据窗口/视口的高度动态调整图像大小
- 动态更改'汉堡包'导航取决于BG图像
- 是否可以仅通过jQuery将图像替换为用户动态选择的另一个图像
- 可在多个相册文件夹中查找的动态图像库
- 使用jquery垂直对齐动态加载的图像
- 根据容器 DIV 中的图像动态调整容器 DIV 的大小
- 如何将任意数量的图像动态添加到源字符串位于数据库中的 Web 窗体
- 将图像动态添加到照片滑动
- 使用 JavaScript 和构造函数的动态变量名称:使用 EaselJS 将图像动态克隆到多个画布
- 可以使用图像URL为图像动态添加替代文本
- 在没有画布的javascript中使用自定义光标图像动态更改光标大小
- CSS背景图像动态调整大小
- 将图像动态添加到HTML字符串中
- 使用javascript将图像动态写入iframe
- 根据背景图像动态调整文本颜色
- 根据图像动态设置画布的宽度和高度
- 在html中将图像动态添加到滑块中
- 将不同尺寸的并排图像动态调整为相同高度
- 将图像动态加载到 jQuery iViewer 中
- 如何使用javascript将图像动态地放在锚标记中