WorkShop--EC-126 Component: Hero Block

HTML页面

<input type="hidden" id="str1" value=${properties.hero_block} />

<sly data-sly-use.clientLib="/libs/granite/sightly/templates/clientlib.html"/>
<sly data-sly-call="${clientLib.js @ categories='hero_block'}"></sly>

<section class="hero hero-block red" id="hero-main" style="">
        <div class="hero-slide-menu">
                <div class="hero-slide-slant"></div>
                <button class="btn-hero-menu"><span class="slide-btn-open">&gt;</span><span class="slide-btn-close">×</span></button>
            <ul class="nav slide-nav" id="hero_name">
            </ul>
        </div>


        <div class="hero-background-container">
            <img class="hero-background large-loaded" id="pic" src="/content/dam/PeficientDigit/forresterwave_homepage_hero_lg.jpg" data-mobile-image="/content/dam/PeficientDigit/forresterwave_homepage_hero_lg.jpg" data-image="/content/dam/PeficientDigit/forresterwave_homepage_hero_lg.jpg">
            <div class="hero-caption"><a href="#"></a></div>
        </div>
        <div class="color-container" style="background: no-repeat right bottom/85%; background-color: #cc1f20;">
            <i class="icon icon-blockangle" style=" color: #cc1f20 " ></i>
            <div class="hero-content">
                <h1 id="hero_name_i">Perficient Trusted to Deliver DPA</h1>

                <p id="hero_tip" style="color:white">Named a "Strong Performer" in the Forrester Wave</p>
                <a id="hero_btn" href="https://blogs.perficient.com/2018/09/28/perficient-among-top-digital-process-automation-service-providers/" class="btn-hero">See Why</a>
            </div>
        </div>

    </section>

type=“hidden” id=“str1” value=${properties.hero_block} />将multifiled字符串隐藏起来,让JS能得到
<sly data-sly-use.clientLibjs的引入要在multifield下面
style=" background-color: #cc1f20;"设置div块的背景色为红色
style=“color:white”设置hero-tip字体颜色为白色

JS代码 hero_block.js

    var json_txt=document.getElementById("str1").value;
	var str_obj="{ \"hero_block\" : [" +json_txt+ "]}";
	console.log(str_obj);
	var obj = eval ("(" + str_obj + ")");       //将JSON字符串转换为JS对象

	function change_image(this_obj) {      //根据参数切换图片和文字
    	var li_name =  document.getElementById('hero_name').getElementsByTagName('li');
        var str=this_obj.id;
        var num =str.substring(str.length-1);
        
        if(this_obj.id!=null){
            var pic_obj = document.getElementById("pic");
			pic_obj.setAttribute("src", obj.hero_block[num].hero_picture); 

            var hero_name= document.getElementById("hero_name_i");
            hero_name.innerHTML= obj.hero_block[num].hero_name;

            var hero_tip=document.getElementById("hero_tip");
            hero_tip.innerHTML= obj.hero_block[num].hero_tip;

			var hero_btn=document.getElementById("hero_btn");
            hero_btn.innerHTML= obj.hero_block[num].hero_button_text;
            hero_btn.setAttribute("href" , obj.hero_block[num].hero_button_url);
        }
    }

	$(document).ready(function(){  //得到父节点,遍历JS对象,插入DOM,添加带参数的函数
		var list = document.getElementById('hero_name');
        for(var i=0;i<obj.hero_block.length;i++){
			var li_name = document.createElement('li'),
        		a_name = document.createElement('a');
            li_name.setAttribute("data-slideid",i);
			a_name.append(""+obj.hero_block[i].hero_name);

			a_name.setAttribute("onclick","change_image(this)");//添加带参数的函数
            a_name.setAttribute("id",'image'+i);
        	li_name.appendChild(a_name);
            list.appendChild(li_name);
        }
	})

在这里插入图片描述在这里插入图片描述

  1. 终于可以不用Sling将multifield转换为对象了
  2. function_name(this) 表示参数是调用者自身,这样就知道是谁调用了函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值