123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- const pic = {
- //预览图片
- preview: function(imgs, img, key) {
- if (imgs && imgs.length > 0) {
- if (key) {
- var temp = [];
- var i;
- for (i in imgs) {
- temp.push(imgs[i][key]);
- }
- imgs = temp;
- }
- // 检测图片是否原图
- img = this.source(img);
- if (imgs.length > 0) {
- for (i in imgs) {
- imgs[i] = this.source(imgs[i]);
- }
- }
- uni.previewImage({
- current: img,
- urls: imgs,
- indicator: 'default',
- loop: true,
- zIndex:999999,
- /*
- longPressActions : {
- itemList: ['发送给朋友', '保存图片', '收藏'],
- success: function(data) {
- console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
- },
- fail: function(err) {
- console.log(err.errMsg);
- }
- }
- */
- });
- }
- },
- //获取原图
- source: function(img) {
- if (img.indexOf('?') != -1) {
- var t = img.split('?');
- img = t[0];
- } else if (img.indexOf('_t') != -1) {
- var t = img.split('.');
- var ext = t[1];
- t = img.split('_t');
- img = t[0] + '.' + ext;
- }
- return img;
- },
- //计算图片宽高比
- getWh: function(imgWidth, imgHeight, containerWidth, containerHeight) {
- let [
- // 用于设定图片的宽和高
- tempWidth,
- tempHeight,
- ] = [
- undefined,
- undefined
- ]
- try {
- imgWidth = parseFloat(imgWidth)
- imgHeight = parseFloat(imgHeight)
- containerWidth = parseFloat(containerWidth)
- containerHeight = parseFloat(containerHeight)
- } catch (error) {
- throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
- }
- if (imgWidth > 0 && imgHeight > 0) {
- //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
- if (imgWidth / imgHeight >= containerWidth / containerHeight) {
- if (imgWidth > containerWidth) {
- // alert('aaaaaaaa')
- tempWidth = containerWidth
- // 按原图片的比例进行缩放
- tempHeight = (imgHeight * containerWidth) / imgWidth
- } else {
- // 按照图片的大小进行缩放
- tempWidth = imgWidth
- tempHeight = imgHeight
- }
- } else { // 原图片的高度必然 > 宽度
- if (imgHeight > containerHeight) {
- tempHeight = containerHeight
- // 按原图片的比例进行缩放
- tempWidth = (imgWidth * containerHeight) / imgHeight
- } else {
- // 按原图片的大小进行缩放
- tempWidth = imgWidth
- tempHeight = imgHeight
- }
- }
- }
- return [tempWidth, tempHeight]
- },
- }
- export default pic;
|