首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用添加的img属性进行响应的图像替换

问使用添加的img属性进行响应的图像替换
EN

Stack Overflow用户
提问于 2013-05-01 23:32:44
回答 4查看 2.1K关注 0票数 2

给出这个html标记...

代码语言:javascript
复制
<figure class="figureFeatured">
    <img 
        width="300" 
        height="213" 
        src="wp-content/uploads/featuredImage-300x213.jpg"
        data-responsive="wp-content/uploads/featuredImage-150x150.jpg"
    />
</figure>

是否可以通过CSS或jQuery将src属性替换为数据响应属性的值?

我只想在特定的设备宽度断点进行替换(css媒体查询或jQuery窗口大小调整)

EN

回答 4

Stack Overflow用户

发布于 2013-05-02 00:08:04

你可以这样做:

把你的分数改成这个

代码语言:javascript
复制
<figure class="figureFeatured"> 
    <img class="yourImgClass" src="wp-content/uploads/featuredImage-150x150.jpg" data-responsive="wp-content/uploads/featuredImage-300x213.jpg"/>

这将首先包括移动图像,符合Responsive的最佳实践。确保在css中将图像设置为max-width: 100%

Javascript方面,这段代码将获得视口宽度,然后在调整窗口大小时交换图像-(将breakPoint变量更改为适合您的需要)

代码语言:javascript
复制
  /*! viewportwidth.js 0.1.1 | Author: Brett Jankord, 2012 | License: MIT */
    /* https://github.com/bjankord/viewportwidth.js */

    function getViewportWidth() {
        var vpw, w = window,
            webkit = (!(window.webkitConvertPointFromNodeToPage == null));

        // Webkit and IE 6-8
        if (webkit || !(typeof (window.innerWidth) == 'number')) {
            vpw = document.documentElement.clientWidth;
        }
        // Everything else
        else {
            vpw = w.innerWidth;
        }
        return vpw;

    };

    // doc ready
    jQuery(function () {
        // xcounter var to indicate if function has run 
        var hasSwapped = 0;

        // function to switch images to larger images

        function swapImages() {

            // for each image
            jQuery('.yourImgClass').each(function () {
                // cache selector
                var $this = jQuery(this);
                // get new src attribute
                var newSrc = $this.attr('data-responsive');
                // assign new image source
                $this.attr('src', newSrc);
                // update counter so function doesn't keep running 
                hasSwapped = 1;
            });
        }

        // set breakpoint width 
        var breakPoint = 850;

        // on resize of window
        jQuery(window).resize(function () {
            // assess the width of the viewport 
            var currentWidth = getViewPortWidth();
            // if the current width is bigger than the required breakpoint and the function has not been run 
            if (currentWidth >= breakPoint && hasSwapped < 1) {
                // run the swap image function 
                swapImages();
            }

        }); // end resize

    // you may also want to check you have the right image on doc ready as well, so you could do this:

        // get viewportWidth
        var initWidth = getViewPortWidth();
        // if the initial width is bigger than the required breakpoint and the function has not been run 
        if (initWidth >= breakPoint && hasSwapped < 1) {
         // run the swap image function 
             swapImages();
        }       


}); // close doc ready

希望这能有所帮助- Mark

票数 1
EN

Stack Overflow用户

发布于 2013-05-02 00:42:03

您也可以尝试响应式Img jQuery插件。它做的正是您需要的,尽管我不确定它在WordPress中是如何工作的。http://responsiveimg.com

票数 0
EN

Stack Overflow用户

发布于 2013-05-10 23:15:44

如果您能够稍微修改一下标记,这是我在responsive/retina s. https://github.com/scottjehl/picturefill上的首选。

您还可以修改JS以匹配您的标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16321250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档