PHP / MooTools 1.2 手风琴助手

MooTools Accordion 插件似乎是人们遇到最多问题的插件。这是一个很棒的插件,所以我明白为什么这么多人想使用它,但我认为这可能是问题的一部分。我认为人们通常在没有足够的 MooTools 或 JavaScript 知识的情况下看到并想要它。这就是我创建 PHP Accordion 类以帮助更轻松、更快速地开发手风琴的原因。

PHP 类

class accordion
{
	var $toggler_class;
	var $item_class;
	var $items;
	var $open;
	
	function accordion($toggler_class = 'toggler',$item_class = 'item')
	{
		$this->items = array();
		$this->toggler_class = $toggler_class;
		$this->item_class = $item_class;
		$this->open = 0;
	}
	
	function add_item($toggler_content = '',$item_content = '',$open = 0)
	{
		$this->items[] = array('toggler'=>$toggler_content,'item'=>$item_content,'open'=>(int)$open);
	}
	
	function build()
	{
		foreach($this->items as $index=>$item)
		{
			$return.= '
						  ';
		  if($item['open']) { $this->open = $index; }
		}
		return $return;
	}
	
	function output()
	{
		echo $this->build();
	}
	
	function output_js($options = '')
	{
		return 'window.addEvent(\'domready\', function () { var accordion = new Accordion($$(\'.'.$this->toggler_class.'\'),$$(\'.'.$this->item_class.'\'), {
					display:'.$this->open.'
					'.($options ? ', '.$options : '').'
					}); });';
	}
	
}

查看有关该类评论的用法。

PHP 的使用

$acc = new accordion('toggler','item');

$acc->add_item('The Beatles','<img src="https://davidwalsh.name/demo/accordion/let-it-be.jpg"  />
<p>The Beatles were a pop and rock group from Liverpool, England. They are one of the most commercially successful and critically acclaimed bands in the history of popular music. The band\'s principal members were John Lennon, Paul McCartney, George Harrison, and Ringo Starr.</p>
<p>The Beatles led the mid-1960s musical "British Invasion" into the United States. Although their initial musical style was rooted in 1950s rock and roll and homegrown skiffle, the group explored genres ranging from Tin Pan Alley to psychedelic rock. Their clothes, styles, and statements made them trend-setters, while their growing social awareness saw their influence extend into the social and cultural revolutions of the 1960s.</p>
<p><a href="http://en.wikipedia.org/wiki/The_beatles">Click here</a> to learn more about The Beatles.</p>');

$acc->add_item('Rod Stewart','<img src="https://davidwalsh.name/demo/accordion/every-picture.jpg"  />
<p>Roderick "Rod" David Stewart, CBE (born January 10, 1945), is a singer and songwriter born and raised in London, England, with Scottish and English parentage. He currently resides in Epping. With a distinctive, raspy voice, Stewart came to prominence in the late 1960s and early \'70s with The Jeff Beck Group and then The Faces and began a solo career in 1969, with his debut album An Old Raincoat Won\'t Ever Let You Down.</p>
<p>With his career in its fifth decade, Stewart has achieved numerous hit singles worldwide, most notably in the UK, where he has garnered six consecutive number one albums and his tally of 62 hit singles include 24 that reached the top 10, six of which gained the number one position. It has been estimated that Stewart\'s album and single sales total more than 250 million, easily earning him a place on the list of best-selling music artists.</p>
<p><a href="http://en.wikipedia.org/wiki/Rod_Stewart">Click here</a> to learn more about Rod Stewart.</p>', 1);

$acc->add_item('Oasis','<img src="https://davidwalsh.name/demo/accordion/oasis.jpg"  />
<p>Oasis are an English rock band that formed in Manchester in 1991. The group was formed by Liam Gallagher (lead vocals), Paul Arthurs (guitar), Paul McGuigan (bass) and Tony McCarroll (drums), who were soon joined by Liam\'s older brother Noel Gallagher (lead guitar, vocals). Oasis have sold more than 50 million albums worldwide, and have had eight UK number one singles. The Gallagher brothers are the band\'s leading songwriters and the only continual members. The present lineup is completed by rhythm/lead guitarist and songwriter Gem Archer, guitarist and songwriter Andy Bell and as-yet unofficial drummer Zak Starkey.</p>
<p><a href="http://en.wikipedia.org/wiki/Oasis_band">Click here</a> to learn more about Oasis.</p>');

echo $acc->output(),'<script type="text/javascript">',$acc->output_js(),'</script>';

创建手风琴时,传入给每个切换器元素的 CSS 类和给每个内容元素的 CSS 类。然后是添加手风琴项目的时候了。对于您要创建的每个项目,传入切换器的内容、项目的内容以及您是否希望给定的项目开始打开。一旦所有项目都进入,您可以调用 output() 输出 XHTML 和 output_js() 输出 MooTools JavaScript。如果您喜欢的话,可以将额外的选项传递给 output_js()

如果您已经知道如何创建 MooTools 手风琴,我不确定该课程对您是否有任何价值。但是,如果您一直在为创建一个而苦苦挣扎,那么这可能是帮助您实现目标的小帮手。

赞(0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏