美文网首页
JS 路上的小问题

JS 路上的小问题

作者: 史梦辰 | 来源:发表于2019-01-06 18:36 被阅读0次

    1.图片加载问题

    想得到图片的宽和高 img.width() img.height
    一定要保证图片加载完成
    使用load事件

    onImg.on("load",function(){ //核心
                            var $this=$(this);
                            var w = $this.width();
                            var h = $this.height();
    }
    

    2.图片按父元素比例缩放

    //图片缩放
    function scalingImage(imgWidth, imgHeight, containerWidth, containerHeight) {
        var containerRatio = containerWidth / containerHeight;
        var imgRatio = imgWidth / imgHeight;
    
        if (imgRatio > containerRatio) {
            imgWidth = containerWidth;
            imgHeight = containerWidth / imgRatio;
        } else if (imgRatio < containerRatio) {
            imgHeight = containerHeight;
            imgWidth = containerHeight * imgRatio;
        } else {
            imgWidth = containerWidth;
            imgHeight = containerHeight;
        }
    
        return { width: imgWidth, height: imgHeight };
    }
    

    3.通过url传递数组的方法

    其中values为数组


    image.png

    4.jquery属性选择器匹配动态变量

    image.png

    其中submitId为动态变量,方法就是'"+submitId+"'这样吧动态变量包裹起来。

    4.safari浏览器下解决Date日期的NAN问题

    image.png

    写了一个如上这个时间友好展示功能,在chrome浏览器下正常,到了safari浏览器下就一直NAN,查了一下资料发现是因为safari浏览不能识别这种类型的字符串转换为时间戳
    new Date("2016-05-31 08:00");
    改成这种类型的字符串就可以了timeStr.replace(/-/g, "/");

    相关文章

      网友评论

          本文标题:JS 路上的小问题

          本文链接:https://www.haomeiwen.com/subject/shzsrqtx.html